An overridden get method on a custom manager passed to get_object_or_404 will not be called.
|Reported by:||anonymous||Owned by:||Claude Paroz <claude@…>|
|Component:||Database layer (models, ORM)||Version:||master|
|Cc:||django@…, anubhav9042@…||Triage Stage:||Ready for checkin|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
If a manager is passed in as the first argument to get_object_or_404, _get_queryset will convert it into a queryset via manager.all().
This means that an overridden get method on the manager passed to get_object_or_404 will not be called. This is rather unintuitive.
# In django.shortcuts __init__.py def _get_queryset(klass): """ Returns a QuerySet from a Model, Manager, or QuerySet. Created to make get_object_or_404 and get_list_or_404 more DRY. """ if isinstance(klass, QuerySet): return klass elif isinstance(klass, Manager): manager = klass else: manager = klass._default_manager return manager.all() def get_object_or_404(klass, *args, **kwargs): #... queryset = _get_queryset(klass) try: return queryset.get(*args, **kwargs) #...
Perhaps it should just be return manager instead of manager.all().
Change History (13)
comment:1 Changed 7 years ago by thatch
- Component changed from Uncategorized to Database layer (models, ORM)
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Triage Stage changed from Unreviewed to Design decision needed
comment:4 Changed 5 years ago by SmileyChris
comment:7 Changed 3 years ago by akaariai
- Triage Stage changed from Design decision needed to Accepted
comment:9 Changed 2 years ago by anubhav9042
- Cc anubhav9042@… added
- Owner changed from nobody to anubhav9042
- Status changed from new to assigned
comment:10 Changed 2 years ago by anubhav9042
- Owner anubhav9042 deleted
- Status changed from assigned to new
comment:12 Changed 3 months ago by timgraham
- Triage Stage changed from Accepted to Ready for checkin