class PublishedManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(status='published')
class Article(models.Model):
title = models.CharField(max_length=200)
status = models.CharField(max_length=20, default='draft')
objects = models.Manager()
published = PublishedManager()
# Hari retrieves articles:
articles_all = Article.objects.all()
articles_published = Article.published.all()
# Later, Hari updates all articles to 'published':
Article.objects.all().update(status='published')
# Now Hari checks the published manager again:
articles_published_after = Article.published.all()Django Model Managers and Custom QuerySets
articles_published_after will return the same articles as before because managers are cached in memory.
articles_published_after will return all articles because the update() changed all statuses to 'published', and the PublishedManager will now include all of them.
articles_published_after will be empty because the custom manager is no longer valid after the bulk update.
articles_published_after will raise an error because you cannot use custom managers after a bulk update.
Show answer & explanationAnswer
Correct answer
articles_published_after will return all articles because the update() changed all statuses to 'published', and the PublishedManager will now include all of them.
Explanation
The custom PublishedManager filters the queryset to only include articles with status='published'. After the update, all articles have status='published', so both querysets will return the same articles.
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
