[Django] #20942: modelformset wrongly assumes foreign keys can't point to proxy models.

14 views
Skip to first unread message

Django

unread,
Aug 19, 2013, 8:49:01 PM8/19/13
to django-...@googlegroups.com
#20942: modelformset wrongly assumes foreign keys can't point to proxy models.
-------------------------------+--------------------
Reporter: cdestigter | Owner: nobody
Type: Uncategorized | Status: new
Component: Forms | Version: 1.5
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
Background: My reusable app [https://github.com/craigds/django-typed-
models django-typed-models] extends proxy models with a type field, so the
proxy model for each instance gets stored in the database.

I wanted to have a ForeignKey pointing to one of those proxy models. The
database level behaviour is the same (it points to the concrete model that
the proxy model proxies), but this makes Django do better type checking
for me:

{{{
class Animal(TypedModel):
name = models.CharField()

class Canine(Animal):
pass

class Feline(Animal):
pass

class BigCat(Feline):
pass

class OtherModel(models.Model):
# at DB level this points to Animal
cat = models.ForeignKey(Feline)
}}}

The idea being that this will work fine:

{{{
othermodel.cat = BigCat(name="simba")
}}}

But this will throw an error:

{{{
othermodel.cat = Canine(name="fido")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File
"/home/cdestigter/checkout/django/django/db/models/fields/related.py",
line 405, in __set__
self.field.name, self.field.rel.to._meta.object_name))
ValueError: Cannot assign "<Canine: fido>": "OtherModel.cat" must be a
"Feline" instance.
}}}

This *almost* works ;)

In fact, it works everywhere except in the admin. I get these errors:

{{{
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/dist-packages/dandelion/geostore/admin.py",
line 7, in <module>
admin.autodiscover()
File
"/home/cdestigter/checkout/django/django/contrib/admin/__init__.py", line
29, in autodiscover
import_module('%s.admin' % app)
File "/home/cdestigter/checkout/django/django/utils/importlib.py",
line 35, in import_module
__import__(name)
File "/usr/lib/python2.6/dist-
packages/dandelion/datasources/admin.py", line 183, in <module>
import_module(admin_module)
File "/home/cdestigter/checkout/django/django/utils/importlib.py",
line 35, in import_module
__import__(name)
File "/usr/lib/python2.6/dist-packages/myproject/myapp/admin.py",
line 18, in <module>
admin.site.register(BigCat, BigCatOptions)
File
"/home/cdestigter/checkout/django/django/contrib/admin/sites.py", line 98,
in register
validate(admin_class, model)
File
"/home/cdestigter/checkout/django/django/contrib/admin/validation.py",
line 189, in validate
validate_inline(inline, cls, model)
File
"/home/cdestigter/checkout/django/django/contrib/admin/validation.py",
line 200, in validate_inline
fk = _get_foreign_key(parent_model, cls.model,
fk_name=cls.fk_name, can_fail=True)
File "/home/cdestigter/checkout/django/django/forms/models.py", line
807, in _get_foreign_key
raise Exception("fk_name '%s' is not a ForeignKey to %s" %
(fk_name, parent_model))
Exception: fk_name 'cat' is not a ForeignKey to <class
'myproject.myapp.models.BigCat'>
}}}


I have a small patch which fixes the issue. I can add tests to it if
someone can accept the concept.

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

Django

unread,
Aug 19, 2013, 8:58:50 PM8/19/13
to django-...@googlegroups.com
#20942: modelformset wrongly assumes foreign keys can't point to proxy models.
----------------------------+--------------------------------------
Reporter: cdestigter | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.5
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1

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

* needs_better_patch: => 1
* has_patch: 0 => 1
* type: Uncategorized => Bug
* needs_tests: => 1
* needs_docs: => 0


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

Django

unread,
Sep 10, 2013, 6:38:06 PM9/10/13
to django-...@googlegroups.com
#20942: modelformset wrongly assumes foreign keys can't point to proxy models.
----------------------------+------------------------------------
Reporter: cdestigter | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.5
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------
Changes (by marfire):

* stage: Unreviewed => Accepted


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

Django

unread,
Sep 24, 2022, 7:59:59 AM9/24/22
to django-...@googlegroups.com
#20942: modelformset wrongly assumes foreign keys can't point to proxy models.
----------------------------------+------------------------------------
Reporter: Craig de Stigter | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.5

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------
Changes (by charlesroelli):

* cc: charlesroelli (added)


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

Django

unread,
Sep 24, 2022, 9:53:49 AM9/24/22
to django-...@googlegroups.com
#20942: modelformset wrongly assumes foreign keys can't point to proxy models.
----------------------------------+------------------------------------
Reporter: Craig de Stigter | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.5

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------

Comment (by OnurBilgic):

With @charlesroelli we found that the Django admin works with foreign keys
pointing to proxy models. The problem was fixed in #32975. The admin error
has not existed anymore. The example code above is manually tested with
TypedModel https://github.com/craigds/django-typed-models library. It
works well.

--
Ticket URL: <https://code.djangoproject.com/ticket/20942#comment:4>

Django

unread,
Sep 24, 2022, 11:19:56 AM9/24/22
to django-...@googlegroups.com
#20942: modelformset wrongly assumes foreign keys can't point to proxy models.
----------------------------------+-------------------------------------

Reporter: Craig de Stigter | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 1.5
Severity: Normal | Resolution: duplicate

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
----------------------------------+-------------------------------------
Changes (by Claude Paroz):

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


Comment:

Thanks a lot for checking this!

--
Ticket URL: <https://code.djangoproject.com/ticket/20942#comment:5>

Reply all
Reply to author
Forward
0 new messages