class BlogPost(models.Model):
title = models.CharField(max_length=200, db_index=True)
author = models.CharField(max_length=100)
published_date = models.DateField()
class Meta:
ordering = ['-published_date']
indexes = [
models.Index(fields=['author', 'published_date'], name='author_date_idx'),
]
constraints = [
models.UniqueConstraint(fields=['title', 'author'], name='unique_title_author'),
]
# Prasath tries to create two posts with the same title but different authors:
BlogPost.objects.create(title='Django Tips', author='Prasath', published_date='2024-01-01')
BlogPost.objects.create(title='Django Tips', author='Hari', published_date='2024-01-02')Django Model Meta Options: Database Indexing and Constraints
The second create() will raise an IntegrityError because the title 'Django Tips' already exists.
Both posts will be created successfully because the UniqueConstraint is on the combination of title and author, and the authors are different.
The db_index on title will prevent the second post from being created.
Only the first post will be created; the second will be silently ignored.
Show answer & explanationAnswer
Correct answer
Both posts will be created successfully because the UniqueConstraint is on the combination of title and author, and the authors are different.
Explanation
The UniqueConstraint applies only to the combination of title AND author fields, not to title alone. Since the authors are different ('Prasath' and 'Hari'), the constraint is not violated and both posts will be created successfully.
All 30 Django Models and Database Design questions
- 1.What is the primary purpose of Django Models?
- 2.Which field type should be used to store an email address in a Django Model?
- 3.What does the Meta class inside a Django Model do?
- 4.Which relationship type is used when one model can have many instances of another model?
- 5.What is the purpose of the `__str__` method in a Django Model?
- 6.Which field attribute makes a field mandatory in Django Models?
- 7.What does the `on_delete` parameter do in a ForeignKey relationship?
- 8.Which of the following is a valid Django field type for storing boolean values?
- 9.What is the default behavior when you don't specify `null=True` for a field in Django?
- 10.Which method is used to retrieve a single object from the database in Django?
- 11.Understanding Django Model Meta Options
- 12.Scenario:
- 13.Django QuerySet Filter vs Get Method
- 14.Coding:
- 15.Scenario:
- 16.Understanding Django Model Inheritance
- 17.Coding:
- 18.Scenario:
- 19.Django Model Manager and QuerySet Methods
- 20.Coding:
- 21.Django Model Inheritance Strategy Selection
- 22.Django QuerySet Optimization with select_related vs prefetch_related
- 23.Django Model Field Choices and Validation
- 24.Django Model Relationships:
- 25.Django Model Signals and Database Consistency
- 26.Django Model Meta Options:
- 27.Django Model Managers and Custom QuerySets
- 28.Django Model Field:
- 29.Django Model Validation:
- 30.Django Model Aggregation and Annotation
