Rewriting this database access query to circumvent 'DISTINCT ON' error, in Django

后端 未结 1 1763
失恋的感觉
失恋的感觉 2021-01-25 11:40

I have two very simple models:

class Link(models.Model):
    description = models.TextField(validators=[MaxLengthValidator(500)])
    submitter = models.ForeignK         


        
1条回答
  •  再見小時候
    2021-01-25 11:56

    You could just take care of the distinct in memory.

    replies_by_link = {}
    replies = Publicreply.objects.filter(answer_to_id__in=link_ids).order_by('answer_to', 'submitted_on')
    
    for reply in replies:
        if reply.answer_to_id not in replies_by_link:
            replies_by_link[reply.answer_to_id] = reply
    

    and then you can access all the replies via replies_by_link.values().

    0 讨论(0)
提交回复
热议问题