Django order_by外键设置模型 [英] Django order_by ForeignKey set models

查看:605
本文介绍了Django order_by外键设置模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下django模型

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

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

我需要一个QuerySet的评论,先通过邮寄,然后按日期排序。
但是帖子必须按照最新的评论进行排序。



如果我可以在QuerySet order_by中使用模型方法,那就像这样:

  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

我需要的订单可以是:

  Comment.objects.all()。order_by 'post_get_last_comment_date','-date')

但不幸的是,order_by中的方法是不允许的。 p>

请帮忙。我可以有这样的订单吗?

解决方案

您可能不会按顺序使用方法查找,因为它们被转换为SQL 。 >

那么为什么不将将get_last_comment_date转换为字段?例如使用信号接收器


$ b $来自django.db.models导入信号的b

  

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
除了Comment.DoesNotExist:
返回

如果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)

现在,您可以: Comment.objects.order_by('post__last_comment_date','-date')


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

这篇关于Django order_by外键设置模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆