Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Generic Foreign keys - Good or Bad considering the SQL performance?

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.

like image 559
Babu Avatar asked Jan 15 '13 08:01

Babu


People also ask

What is Django generic foreign key?

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.

What is Django Genericrelation?

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.

What is Generic relation?

Generic relationships are abstraction patterns used for structuring information across application domains. They play a central role in information modeling.


1 Answers

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.

like image 91
L42y Avatar answered Nov 06 '22 12:11

L42y