Opened 7 years ago

Last modified 7 years ago

#20942 new Bug

modelformset wrongly assumes foreign keys can't point to proxy models.

Reported by: Craig de Stigter Owned by: nobody
Component: Forms Version: 1.5
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no


Background: My reusable app 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):

    class Feline(Animal):

    class BigCat(Feline):

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

The idea being that this will work fine: = BigCat(name="simba")

But this will throw an error: = Canine(name="fido")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/cdestigter/checkout/django/django/db/models/fields/", line 405, in __set__,
    ValueError: Cannot assign "<Canine: fido>": "" 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/", line 7, in <module>
      File "/home/cdestigter/checkout/django/django/contrib/admin/", line 29, in autodiscover
        import_module('%s.admin' % app)
      File "/home/cdestigter/checkout/django/django/utils/", line 35, in import_module
      File "/usr/lib/python2.6/dist-packages/dandelion/datasources/", line 183, in <module>
      File "/home/cdestigter/checkout/django/django/utils/", line 35, in import_module
      File "/usr/lib/python2.6/dist-packages/myproject/myapp/", line 18, in <module>, BigCatOptions)
      File "/home/cdestigter/checkout/django/django/contrib/admin/", line 98, in register
        validate(admin_class, model)
      File "/home/cdestigter/checkout/django/django/contrib/admin/", line 189, in validate
        validate_inline(inline, cls, model)
      File "/home/cdestigter/checkout/django/django/contrib/admin/", 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/", 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.

Attachments (1)

20942-no-tests.diff (1.2 KB) - added by Craig de Stigter 7 years ago.

Download all attachments as: .zip

Change History (3)

Changed 7 years ago by Craig de Stigter

Attachment: 20942-no-tests.diff added

comment:1 Changed 7 years ago by Craig de Stigter

Has patch: set
Needs tests: set
Patch needs improvement: set
Type: UncategorizedBug

comment:2 Changed 7 years ago by Kevin Christopher Henry

Triage Stage: UnreviewedAccepted
Note: See TracTickets for help on using tickets.
Back to Top