I have a model A
which contains a generic foreign key relation with limit choices to 3 other models(consider them as B
, C
and D
) in the same app. And I know the limitations of generic foreign keys that we can't use filter
or get
or anyother queryset operations.
So to achieve something like this, A.objects.filter(generic_object__name="foo")
I have to filter B, C and D's objects first as queryset, iterate over them and use the generic reverse relation to get the A
objects as list(not queryset).
I'm not sure about how it'll affect the SQL performace on database as the querying is not direct.
PS: I need to use the generic foreignkeys, so please suggest for any SQL improvement rather than redesigning of models.
Using Django 1.4.3 and Postgres.
In Django, a GenericForeignKey is a feature that allows a model to be related to any other model in the system, as opposed to a ForeignKey which is related to a specific one.
Basically it's a built in app that keeps track of models from the installed apps of your Django application. And one of the use cases of the ContentTypes is to create generic relationships between models.
Generic relationships are abstraction patterns used for structuring information across application domains. They play a central role in information modeling.
I'd like to quote some words from David Cramer: developer of Disqus, Django commiter
Generic relations are fine. They are not slow, just more difficult to manage in your code base.
I saw many people tell others don't use generic relations because it's slow, but never tell how it's slow.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With