简体   繁体   English

Django-对外键进行排序和过滤

[英]Django - Order and filter on Foreign Key

What is the proper way to order and filter on a ForeignKey key on model like this one? 在像这样的模型上对ForeignKey键进行排序和过滤的正确方法是什么?

class Ticket(models.Model):
    name = models.CharField(max_length=250)
    created_time = models.DateTimeField(auto_now_add=True)

    def current(self):
        return TicketUpdate.objects.filter(ticket=self.id).first()

class TicketUpdate(models.Model):
    ticket = models.ForeignKey(Ticket)
    description = models.TextField()
    status = models.CharField(max_length=1, choices=STATUSES, default='N')
    type = models.CharField(max_length=1, choices=TYPES, default='N')
    priority = models.CharField(max_length=1, choices=PRIORITIES, default='D')
    created_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-created_time']

Every ticket has one (at least) or more TicketUpdate related. 每张票证都有一个(至少)或多个与票务更新相关的信息。

I want to: 我想要:

  • list all the ticket from Ticket and show their current status (the newest TicketUpdate item) 列出票证中的所有票证并显示其当前状态(最新的票证更新项)
  • order the list by TicketUpdate__created_time or Ticket__created_time 通过TicketUpdate__created_timeTicket__created_time订购列表
  • filter the list by TicketUpdate__priority or TicketUpdate__type or TicketUpdate__status TicketUpdate__priorityTicketUpdate__typeTicketUpdate__status筛选列表

I am not sure if I have to make a Queryset on Ticket or on TicketUpdate. 我不确定是否必须在票证或票证更新上进行查询。

I am not even sure if I need that current() function on the Ticket model: I use it on my ListView to retrieve the TicketUpdate info. 我什至不确定我是否需要在Ticket模型上使用current()函数:我在ListView上使用它来检索TicketUpdate信息。

With

Ticket.objects.all().order_by('ticketupdate__created_time')

I get a list ordered but with too many entries. 我得到了订购的清单,但条目过多。

Everything must run on a sqlite3 db too. 一切也必须在sqlite3 db上运行。

Edit: This filter queryset 编辑:此过滤器查询集

Ticket.objects.filter(ticketupdate__status='New').values('name', 'ticketupdate__status').order_by('ticketupdate__created_time')

return all the existing ticket, since all of them have an initial New status. 返回所有现有票证,因为它们都具有初始的“新”状态。 I keep the whole history of a ticket: New , Working , Fixed , NotFixed , Working ("Working" again: that fix did not work and the user reopen the ticket) 我保留了票证的整个历史记录: NewWorkingFixedNotFixedWorking (再次是“ Working”:该修复程序无效,用户重新打开了票证)

So I want to filter just by the latest status: I need all the Working but what if a ticket have both Working and Fixed? 因此,我只想按最新状态过滤:我需要所有“ 工作”状态,但是如果票证同时具有“工作”状态和“固定”状态怎么办? I just need to filter on the latest. 我只需要筛选最新的。

Yeah you don't really need that current method. 是的,您真的不需要那种当前方法。

You could just get the queryset and take the first item; 您可以只获取查询集并获取第一项。

Ticket.objects.all().order_by('ticketupdate__created_time')[0]

Or take a more django based approach of; 或者采用更基于django的方法;

Ticket.objects.all().latest('ticketupdate__created_time')

And if you want to filter things; 而且,如果您想过滤事物;

Ticket.objects.filter(ticketupdate__status='status').order_by(
    'ticketupdate__created_time')

Ticket.objects.filter(ticketupdate__priority='high').latest(
    'ticketupdate__created_time')

You can try: 你可以试试:

For list all the ticket from Ticket and show their current status (the newest TicketUpdate item): 用于列出票证中的所有票证并显示其当前状态(最新的票证更新项目):

For List: 对于列表:

Ticket.objects.values('name', 'ticketupdate__status')

For latest Ticket Update Object: 对于最新的票证更新对象:

TicketUpdate.objects.latest('ticket__created_time')

For ordering the list by TicketUpdate__created_time or Ticket__created_time: 要在TicketUpdate__created_time或Ticket__created_time之前订购列表:

Ticket.objects.values('name', 'ticketupdate__status').order_by('ticketupdate__created_time')

or 要么

Ticket.objects.values('name', 'ticketupdate__status').order_by('created_time')

For filtering the list by TicketUpdate__priority or TicketUpdate__type or TicketUpdate__status: 要按TicketUpdate__priority或TicketUpdate__type或TicketUpdate__status筛选列表:

Ticket.objects.filter(ticketupdate__status='your status choice').values('name', 'ticketupdate__status').order_by('ticketupdate__created_time') # simillary priority

EDIT 编辑

As I can see, status field in the TicketUpdate is a Character Field, so that A TicketUpdate object can't have both Working or New Status. 如我所见, TicketUpdate中的status字段是一个字符字段,因此TicketUpdate对象不能同时具有Working状态或New状态。 If you want to get the object which was lastly updated/created, consider adding auto_now to field created_time and whenever you query using: 如果要获取最近更新/创建的对象,请考虑将auto_now添加到字段created_time并在每次使用以下方法查询时添加:

Ticket.objects.filter(ticketupdate__status='New').values('name', 'ticketupdate__status').order_by('ticketupdate__created_time')

You will get the list sorted in such order that the last updated/created object will be the first index of that list. 您将获得按此顺序排序的列表,以使最后更新/创建的对象成为该列表的第一个索引。 Also: 也:

Ticket.objects.filter(ticketupdate__status='New').values('name', 'ticketupdate__status').order_by('ticketupdate__created_time').first()

Will return the latest Ticket data. 将返回最新的Ticket数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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