Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Casting foreign keys of foreign keys #512

Open
pierregm opened this issue Mar 3, 2022 · 0 comments
Open

Casting foreign keys of foreign keys #512

pierregm opened this issue Mar 3, 2022 · 0 comments

Comments

@pierregm
Copy link

pierregm commented Mar 3, 2022

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 to make sure I can easily access the special field of my offer on gizmo_a ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant