简体   繁体   English

如何根据python中的字典值按日期范围过滤,否则按字典过滤列表?

[英]How do I filter by a date range based on a dictionary value in python, otherwise filter a list by a dictionary?

I have a dictionaries that look like this: 我有一个看起来像这样的字典:

s_dates_dict = {17: datetime.date(2009,21,9,0,24), 19: datetime.datetime(2011,12,1,19,39,16), ....}  
e_dates_dict = {17: datetime.date(2010,25,9,10,24), 19: datetime.datetime(2012,1,11,17,39,16), ....}

I want to use these dictionaries to find the next record of type (A or B) after that date for each record. 我想使用这些词典为该记录找到该日期之后的下一个类型(A或B)的记录。 So it seems that I would be best off creating a query similar to below: 因此,似乎最好不要创建类似于以下内容的查询:

next_record_list = Tasks.objects.filter(date__range(s_dates_dict,e_dates_dict), client__in=client_list, task_type__in=[A,B])  

Whereby the dictionaries would cause a dynamically changing range to the pk referenced. 因此,词典将导致动态变化范围到所引用的pk。 I haven't found anything suggesting this is possible or efficient, so I am guessing the next best would be creating a list by eliminating the date range filter and then iterating a dictionary of the oldest values using a for statement and cutting off iteration of each record at the date referenced in dates_dict. 我还没有发现任何暗示这可能或有效的建议,所以我猜测下一个最佳选择是创建一个列表,方法是取消日期范围过滤器,然后使用for语句迭代最旧值的字典,并切断每个值的迭代记录dates_dict中引用的日期。 But I haven't figured out a method to do this either. 但是我也没有想办法做到这一点。 Could I get some suggestions of how to do this, or a totally different better method? 我可以从中获得一些建议,或者完全不同的更好方法吗? Thanks. 谢谢。

EDIT: 编辑:

client_list is a list of client objects. client_list是客户端对象的列表。

Here is some of the models.py: 这是一些models.py:

class Client(models.Model):
    user_name = models.CharField

class Task(models.Model):
    client = models.ForeignKey(
                               'Client',
                               )
    task_type = models.ForeginKey(
                                  'Task_Type',
                                  )
    date = models.DateTimeField(
                                 default = datetime.now(),
                                 blank = True,
                                 null = True,
                                 )

class Task_Type(models.Model):
    name = models.CharField  

I think I understand now. 我想我现在明白了。 I am assuming client_list is actually a list of client objects. 我假设client_list实际上是客户端对象的列表。 Someone may come up with a more efficient solution, but I would try something like this: 有人可能会想出一个更有效的解决方案,但我会尝试这样的事情:

next_record = {}
for client in client_list:
    s = s_dates_dict[client.pk]
    e = e_dates_dict[client.pk]
    next_record[client.pk] = client.tasks_set.filter(date__range(s,e), task_type__in=[A,B])

This gives you a dict ( next_record ) where each key corresponds to a list of tasks. 这为您提供了一个dict( next_record ),其中每个键对应一个任务列表。 I am assuming there is a ForeignKey relationship between your Tasks and Client models. 我假设您的TasksClient模型之间存在ForeignKey关系。

Hopefully this works, if not, can you please post the code for your models? 希望这行得通,如果不能,您可以为您的模型发布代码吗?

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

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