Opened 15 years ago

Last modified 15 years ago

#10108 closed

Two classes with a common superclass have multiple ForeignKeys from one to the other if the one has a single Foreign Key to the other. — at Initial Version

Reported by: mail@… Owned by: nobody
Component: Core (Other) Version: dev
Severity: Keywords: foreign key, inheritance, ForeignKey, OneToOne
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Given three classes as follows:

class Item(models.Model):

class Computer(Item):

class CPU(Item):
    computer = models.ForeignKey(Computer)

When inlining CPU into Computer in the admin interface the following exception is raised:

<class 'comprec.inventory.models.CPU'> has more than 1 ForeignKey to <class 'comprec.inventory.models.Computer'>

This is caused by the OneToOne field created implicitly by the inheritance as mentioned on http://docs.djangoproject.com/en/dev/ref/models/fields/#onetoonefield conflicting with the defined ForeignKey on the model. This leads to unexpected behaviour as it is expected that the single defined ForeignKey field is used in the admin form. The error appears to be caused by the _get_foreign_key function in forms/models.py which uses isinstance(f, ForeignKey) on the fields on the model with the ForeignKey to find which field to use but this returns true on OneToOne fields, not just ForeignKey fields.

A traceback of the error is available at http://dpaste.com/112033/.

Change History (0)

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