Hey, this is a quick one. I have two models, Post and Term and I'd like to be able to tag and categorize (taxonomy) posts as well as other (future) models. My Post model has the following fields: title, content, published (date) and my Term is declared like this:
class Term(models.Model):
taxonomy = models.CharField(max_length=255)
name = models.CharField(max_length=255)
slug = models.SlugField(max_length=50)
Then I have a TermRelation model that sticks Terms to Posts and other models, like this:
class TermRelation(models.Model):
term = models.ForeignKey(Term)
object_id = models.PositiveIntegerField()
content_type = models.ForeignKey(ContentType)
content_object = generic.GenericForeignKey()
Everything works as expected but my question is the following. I'd like to create an archive of posts in a certain category, and order the posts by their publish date. This is what I'm trying to do:
ctype = ContentType.objects.get_for_model(Post)
relations = TermRelation.objects.filter(content_type__pk=ctype.id)
And it works fine, although it's sorted by the relation PK I guess. When I try to do the following:
relations = TermRelation.objects.filter(content_type__pk=ctype.id).order_by('content_object__published')
I get an error saying that there's no content_object field in TermRelation. I know there must be a way to solve it. Any ideas?
Thanks ~ K