In Django, how can you add a column to an existing model’s database table?
- Define a new model and migrate the changes using Django's makemigrations and migrate commands.
- Use the add_column method provided by the Django models module.
- Use the ALTER TABLE SQL command directly on the database.
- You cannot add a column to an existing model in Django.
In Django, you define a new model field within the existing model, then create a migration using makemigrations and apply it using migrate to add a new column to the database table associated with the model. Direct SQL commands should be avoided to maintain consistency and manageability.
Loading...
Related Quiz
- You are developing a Django application with a focus on high performance. How would you optimize database queries in views to reduce the load time?
- In Python, which loop is used to iterate over a sequence of elements?
- In Django, what is the role of a "view"?
- What will be the result of the comparison 5 != 6 in Python?
- You are tasked with finding the common elements between two large datasets. Which algorithmic approach would be the most efficient?