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.
|Reported by:||mail@…||Owned by:||nobody|
|Severity:||Keywords:||foreign key, inheritance, ForeignKey, OneToOne|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Description (last modified by kmtracey)
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/.