Generic Foreign Key

21 views
Skip to first unread message

dtdave

unread,
Jul 21, 2016, 3:43:17 AM7/21/16
to Django users
I have two models notes and counties which are used across multiple models. At the moment the realtionship is done using a foreign key.

I am getting conflicting advice on whether to use a Generic Foreign Key. Could someone please enlighen me as to the best practice?

Many thanks in advance

ludovic coues

unread,
Jul 21, 2016, 6:14:55 AM7/21/16
to django...@googlegroups.com
Generic Foreign Key solve a very specific problem. Foreign key to
arbitrary models. Like a tagging system, with the ability to set a tag
to user, article, comment, media in a blog.

For your use, as long as no models can link to notes or counties and
not both, simple foreign key are fine.
And even then, it might be easier to have a foreign key to both and
maybe a method on your models to return one without testing which one
is None.

Generic Foreign Key have some non-trivial drawback. For exemple, you
can't use them in a filter on a queryset.

2016-07-21 9:43 GMT+02:00 'dtdave' via Django users
<django...@googlegroups.com>:
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/37577063-87bf-4460-a6fb-7a17d74dc79c%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

David Turner

unread,
Jul 21, 2016, 7:15:44 AM7/21/16
to django...@googlegroups.com
Hi

Thanks for this.
So basically if I am using the county Foreign key in a few different models I am fine. I am not sure what you meant by this
"as long as no models can link to notes or counties and
not both, simple foreign key are fine."

You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/MAoN1LNDMEg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

ludovic coues

unread,
Jul 21, 2016, 9:14:23 AM7/21/16
to django...@googlegroups.com
I was thinking of the case of one model which can have a foreign key
to one of two models.
Like a title model that can have a foreign key to a page model or an
article model.

2016-07-21 13:15 GMT+02:00 'David Turner' via Django users
<django...@googlegroups.com>:
> https://groups.google.com/d/msgid/django-users/CALwQ%2B-ueLUWTc6taeQAES-krh_ibH4ZfnFLufc6R6OwDz1oOwA%40mail.gmail.com.

David Turner

unread,
Jul 21, 2016, 9:22:47 AM7/21/16
to django...@googlegroups.com
Okay thanks for the clarification

Reply all
Reply to author
Forward
0 new messages