Django Generic Relations Made Easier
I’ve used GenericForeignKey on several sites for tagging. Recently I ran into an issue where the admin interface provided by Django will be used by one of my customers. I thought about changing GenericForeignKey to ManyToManyField. The problem was that this is a hack, I would have to include a ForeignKey back to each type of class I was tagging. Not good if I ever add a new class to tag (which I have already).
Then I though about a neat trick I saw where the developer added a CharField called tag_text and then overloaded the save() function to insert the contents of the field into the tag relation. CharField is just a place holder for the admin interface to be able to present tags to the user. The problem here is that tags must be strictly edited via admin otherwise the placeholder field and the actual tags get out of synch. Again not good for extensibility.
My final and best option is to use a Django admin tweak I found on Net-X. This patch for Django admin is a must have if you are using GenericForeignKey. It makes the data so easy to manipulate by translating all those messy generic ids into nice click and choose textual elements. I can spend pages explaining it so just visit this page, bookmark it whatever, I expect you’ll need it sooner or later (at least until newforms).
Visit: Django generic relations made easier – Net-X
Minor corrections to the Net-X instructions:
- Path "django/contrib/admin/templates/base.html" is sometimes "django/contrib/admin/templates/admin/base.html"
- In the base_site.html the 1st line is missing "{% extends “admin/base.html” %}"
More from Aware Labs
- Constructive reasons to use Django instead of Rails (Proxied)
- Using Django Models In Batch Jobs
- What Is Your Version Of Django
- UnicodeDecodeError Exception Fix On Templates
- Clearing Django Form Fields One By One
Aware Labs Recommends
- Popularizing Django — Or Reusable apps considered harmful. (USwaretech)
- Django’s tipping point (Antonio Cangiano)
- An Interview with Jacob Kaplan-Moss – Creator of Django (USwaretech)
-
Joe Murphy
-
pkenjora

![Recommend [AwareLabs]](http://s3.amazonaws.com/arkayne-media/img/badge/logo-recommend-badge-medium.png)