django_polymorphic icon indicating copy to clipboard operation
django_polymorphic copied to clipboard

Polymophic primary key as OneToOneField

Open vrde opened this issue 14 years ago • 1 comments

Is it possible to use a OneToOneField as primary key in a PolymorphicModel? For example, take a look to the following snippet: class Food(models.Model): id = models.AutoField(primary_key=True)

class PreparedFood(PolymorphicModel): id = models.OneToOneField(Food, primary_key=True)

If we register the models to the admin interface, they will result in an exception:

  • Caught AttributeError while rendering: type object 'PreparedFood' has no attribute 'polymorphic_primary_key_name'

I think the "problem" reside in polymorphic.base.py:82

    for f in new_class._meta.fields:
        if f.primary_key and type(f)!=models.OneToOneField:
            new_class.polymorphic_primary_key_name=f.name
            break

Apparently if the primary_key is a OneToOneField the polymorphic_primary_key_name is not defined. What is the reason behind the type(f)!=models.OneToOneField check?

Thank you.

vrde avatar Apr 16 '11 14:04 vrde

Edit: On closer inspection what vrde said.

macropin avatar Jul 23 '13 05:07 macropin