stalk
stalk

Reputation: 12054

Django order_by ForeignKey set models

I have following django models

class Post(models.Model):
    title = models.CharField(max_length=240)

class Comment(models.Model):
    post = models.ForeignKey(Post)
    date = models.DateTimeField(auto_now_add=True)

I need a QuerySet of comments, ordered first by post, then by date. But posts must be ordered by its latest comment.

If i could use model methods in QuerySet order_by, it would be like this:

class Post(models.Model):
    title = models.CharField(max_length=240)

    def get_last_comment_date(self):
        return self.comment_set.order_by('-date')[0].date

And the ordering, that i needed, could be:

Comment.objects.all().order_by('post__get_last_comment_date', '-date')

But unfortunately, methods in order_by are not allowed.

Please, help. Can i have such ordering?

Upvotes: 0

Views: 1422

Answers (1)

jpic
jpic

Reputation: 33410

You may not use methods in order_by lookups because they are converted to SQL.

So, why not convert get_last_comment_date into a field ? e.g. using a signal receiver:

from django.db.models import signals

class Post(models.Model):
    title = models.CharField(max_length=240)
    last_comment_date = models.DateField(null=True, blank=True)

def post_last_comment_date(sender, instance=None, **kwargs):
    try:
        last_comment_date = self.comment_set.order_by('-date')[0].date
    except Comment.DoesNotExist:
        return

    if last_comment_date != comment.post.last_comment_date:
        comment.post.last_comment_date = last_comment_date
        comment.post.save()

signals.post_save.connect(post_last_comment_date, sender=Comment)

Now, you can: Comment.objects.order_by('post__last_comment_date', '-date')

Upvotes: 4

Related Questions