Foreign key of foreign key in django-polymorphic

156 Views Asked by At

I have the following (simplified) django and django-polymorphic models:

class GenericOffer(PolymorphicModel):
    pass
class OfferA(GenericOffer):
    special = models.IntegerField()
class OfferB(GenericOffer):
    pass

class GenericProduct(PolymorphicModel):
    offer = models.ForeignKey(to=GenericOffer)
class ProductA(GenericProduct):
    pass
class ProductB(GenericProduct):
    pass

class Gizmo(models.Model):
    product = models.ForeignKey(to=GenericProduct)

from which I can create the following instances:

offer_a = OfferA()
product_a = ProductA(offer=offer_a)
gizmo_a = Gizmo(product=product_a)

Now, I have:

assert product_a.offer == offer_a
assert gizmo_a.product == product_a

But:

assert gizmo_a.product_a.offer != offer_a
assert gizmo_a.product_a.offer.get_real_instance() == offer_a

That is, the foreign key linked to a foreign key of my Gizmo is not automatically cast to its proper type.

  • Is it the expected behavior?

  • Should I use a GenericRelation instead of a ForeignKey in my Gizmo ?

Thanks for any advice

0

There are 0 best solutions below