python - 在 Django 中获取最新的不同对象

标签 python django distinct tastypie

目前我有一个简单的消息聊天应用。

class Person(models.Model):
     user = models.OneToOneField(User, primary_key=True)
     sex = models.CharField(max_length=1, null=True, blank=True)

class Message(models.Model):
     sender = models.ForeignKey(Person, related_name= 'sent')
     recipient = models.ForeignKey(Person, related_name = 'received')
     created = models.DateTimeField(auto_now_add=True, null = True, blank = True)
     message = models.CharField(max_length=500)
     conversation_id = models.CharField(max_length = 90)

我使用 Django Tastypie 作为我的休息 API。我想创建一个收件箱,类似于 Facebook 的收件箱,其中对话按最新排序。最近的消息对话会显示在收件箱的顶部。

“conversation_id”是发件人用户名和收件人用户名的组合。

例如,如果用户 Kyle 向用户 Alex 发送消息,则 conversation_id 将为“alexkyle”。

如果 Alex 给 Kyle 发消息,它仍然是“alexkyle”。如果亚历克斯给鲍勃发消息,它将是“alexbob”。

我已将其设置为 conversation_id 将始终按照唯一的字母顺序排列。 conversation_id 是我用来区分对话的。

我有以下资源:

class MessageResource(ModelResource):
    sender = fields.ForeignKey(PersonResource, 'sender', full =True)
    recipient= fields.ForeignKey(PersonResource, 'recipient', full=True)
    class Meta:
        queryset = Message.objects.all()
        allowed_methods = ['get']
        resource_name = 'message-list'
        fields = ['message', 'sender', 'recipient', 'created', 'id', 'conversation_id', 'username']
        authorization = Authorization()
        authentication = BasicAuthentication()
        include_resource_uri = False

    def get_object_list(self, request):
        return super(MessageResource, self).get_object_list(request).filter(Q(sender__user = request.user) | Q(recipient__user = request.user)).distinct('conversation_id').order_by('conversation_id','-created')

但是这有效!它按字母顺序发送数据,而不是按最近的对话发送数据。为了使用 distinct,我必须使用 order_by。问题是我不希望数据按字母顺序排列。我希望它按照最新对话的顺序排列。我该怎么做呢?

最佳答案

你的最后一行应该是这样的

return super(MessageResource, self).get_object_list(request).filter(Q(sender__user = request.user) | Q(recipient__user = request.user)).distinct('conversation_id').annotate(last_created=MAX(created)).order_by('-last_created')

在代码中,我使用了 MAX 方法,应该使用以下方法导入:

from django.db.models import Max

顺便说一句,史蒂夫说得有道理! conversation_id 对我来说似乎有点问题。

更新 1

要使用带注释的分组,应调用 values 或 values_list 方法:

return super(MessageResource, self).get_object_list(request).filter(Q(sender__user = request.user) | Q(recipient__user = request.user)).values('conversation_id').annotate(last_created=Max(created), distinct=True).order_by('-last_created')

关于python - 在 Django 中获取最新的不同对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19799354/

相关文章:

mysql - GROUP BY x 与 DISTINCT( x )

mysql,选择Distinct仍然返回多个项目

python - Python 中的素数和完美平方检查器

python - python 的 BaseHTTPRequestHandler 中 connfd(在 C 中创建的套接字)的等价物在哪里

python - 一方面 KFlold 与 KFold 之间的差异,另一方面 shuffle=True 和 RepeatedKFold 在 sklearn 中

django - 如何在 django init_data 上创建默认用户?

regex - Oracle - 当只对不同的匹配感兴趣时,优化 CLOB 列上所有正则表达式匹配的循环

Python-将科学计数法字符串转换为保留小数位的 float

django - 为什么编辑模型实例后无法保存它们?

python - 无法使用 python3 和 sqlite3 安装 pyspatialite