[Django] #23710: ModelForm is not using the plain manager for foreign keys

17 views
Skip to first unread message

Django

unread,
Oct 23, 2014, 9:59:29 PM10/23/14
to django-...@googlegroups.com
#23710: ModelForm is not using the plain manager for foreign keys
-------------------------------+--------------------
Reporter: eagle-r | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.7
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
According to the documentation, Django will use an instance of the plain
manager instead of the default manager for related fields (see
https://docs.djangoproject.com/en/1.7/topics/db/managers/#using-managers-
for-related-object-access).

However, the ModelForm initialisation code is using the default manager
instead when initialising the fields during loading (see
django/db/models/fields/related.py:1749)

So, if you have a model that is using a custom default manager, the
modelform attempts to use this manager instead leading to some very
unexpected side effects particularly when running the server and loading
up the code initially.

--
Ticket URL: <https://code.djangoproject.com/ticket/23710>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 24, 2014, 1:10:37 PM10/24/14
to django-...@googlegroups.com
#23710: ModelForm is not using the plain manager for foreign keys
-------------------------------+--------------------------------------

Reporter: eagle-r | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.7
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by collinanderson):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

The default manager is the first manager defined, not necessarily a
"plain" manager, right?
https://github.com/django/django/blob/d49183e4bed8ecf21faad44372ea15ff00f781b5/django/db/models/manager.py#L127-129

--
Ticket URL: <https://code.djangoproject.com/ticket/23710#comment:1>

Django

unread,
Oct 24, 2014, 9:57:02 PM10/24/14
to django-...@googlegroups.com
#23710: ModelForm is not using the plain manager for foreign keys
-------------------------------------+-------------------------------------

Reporter: eagle-r | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.7
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: | Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* component: Uncategorized => Database layer (models, ORM)


Comment:

Collin, yes that's what's documented in the
[https://docs.djangoproject.com/en/1.7/topics/db/managers/#default-
managers default managers] docs.

I am not sure about this. The behavior doesn't seem to be well defined as
I couldn't find any documentation about this and no tests fail if I make
this change (please correct me if this is the wrong line; this is the
closest line I could find on stable/1.7.x to the one you referenced):

{{{
#!diff
diff --git a/django/db/models/fields/related.py
b/django/db/models/fields/related.py
index d87a922..0b4d726 100644
--- a/django/db/models/fields/related.py
+++ b/django/db/models/fields/related.py
@@ -1759,7 +1759,7 @@ class ForeignKey(ForeignObject):
(self.name, self.rel.to))
defaults = {
'form_class': forms.ModelChoiceField,
- 'queryset': self.rel.to._default_manager.using(db),
+ 'queryset': self.rel.to._base_manager.using(db),
'to_field_name': self.rel.field_name,
}
defaults.update(kwargs)
}}}
On the other hand, I could see the current behavior as expected and
changing it could cause problems for people relying on it.

--
Ticket URL: <https://code.djangoproject.com/ticket/23710#comment:2>

Django

unread,
Nov 26, 2014, 6:17:47 PM11/26/14
to django-...@googlegroups.com
#23710: ModelForm is not using the plain manager for foreign keys
-------------------------------------+-------------------------------------
Reporter: eagle-r | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 1.7
(models, ORM) | Resolution: needsinfo

Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* status: new => closed
* resolution: => needsinfo


Comment:

Lacking any follow-up from the reporter, I'm going to close as "needs
info". Feel free to reopen if you can describe more about the problem
here.

--
Ticket URL: <https://code.djangoproject.com/ticket/23710#comment:3>

Reply all
Reply to author
Forward
0 new messages