#32640: Non-manager instance assigned to model class' objects is silently
ignored
-------------------------------------+-------------------------------------
Reporter: Shai | Owner: nobody
Berger |
Type: Bug | Status: new
Component: Database | Version: 2.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 1
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Consider this models file:
{{{#!python
from django.db import models
class MyManager(models.Manager):
def get_queryset(self):
return self.none()
class MyModel(models.Model):
one = models.IntegerField()
objects = MyManager # Note: Missing parentheses
}}}
The assignment of a manager class, rather than manager instance, is an
easy mistake to make. I know, because I've made it. In a private
discussion, Carlton Gibson suggested another variant of this:
{{{#!python
class MyOtherModel(models.Model):
...
objects = models.Manager.from_queryset(SomeQueryset())
# The above, too, is missing a pair of parentheses
}}}
But what should Django do?
There's two possible behaviors I would consider reasonable:
- Trust the user, and rely on Duck Typing. This would blow up very fast in
this case, as any method invocation would be missing the {{{self}}}
argument.
- Raise an error on model creation, or at least in {{{check}}}.
What Django does instead, is ignore the assignment and use a
{{{models.Manager}}} instance.
Two points to clarify that this is indeed a bug:
- Assigning any object which isn't a {{{models.Manager}}} instance -- say,
the number 17 -- gets the same result.
- This only happens if only one manager is defined. If another manager is
defined, the assignment stands as written (the first "reasonable" behavior
described above).
I found this on 2.2, it is still this way on master.
--
Ticket URL: <https://code.djangoproject.com/ticket/32640>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/048.0ae4e734f527ffe2b79b509bab3547fe%40djangoproject.com.