简体   繁体   中英

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?

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 ? eg 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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM