Nobody Tells Me Anything… Ever…
September 29th, 2008… or is that I’m not listening? You tell me…
One of the most important features of open source software, is not the fact that you can fix bugs yourself, or the fact that it’s free, it’s the incredible educational good it effortlessly spreads. It was whilst I was looking at someone else’s Django code that I discovered this…
from django.db import models from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.generic import GenericForeignKey class MyListingItem(models.Model): content_type = models.ForeignKey(ContentType) object = GenericForeignKey() object_id = models.IntegerField()
… I’ve been trying to find a way to do this for years. I mistakenly thought that SQL was “a bit broke” because it didn’t naturally do this. What this does is let you build a list of any old objects so that you can easily have different methods for each object types. It’s effectively storing objects in an object-oriented way rather than relating objects in a relational database way. So in a TumbleLog I might store Delicious items, Tweets, Blog Posts etc. and each would have their own methods for being fetched, displayed etc. This, to me, is much more natural and sensible than the way I’ve been doing it.
If you know what I’m talking about you probably already know about this. If you haven’t a clue what I’m talking about it really doesn’t matter except to say, it’s amazing how you can literally labour under a small misapprehension for years that sends you miles off track… or is it just me (and how would I know?).

