我在django 1.11中的admin中提出了解决方案-在2个月(60天)内,在管理面板上的即将到来的活动中显示在列表顶部。休息时间(也即将出现,但例如3个月至90天)如下所示。
我想测试我的解决方案,所以我写了单元测试。但是当我按以下顺序打印对象时:Event title 1, Event title 2, Event title 3
,应该是Event title 1, Event title 3, Event title 2
。
我不知道我在哪里犯错了。
这是admin.py中的queryset
def get_queryset(self, request):
next_two_months = datetime.today() + timedelta(days=60)
not_three_months = datetime.today() + timedelta(days=90)
qs = super(EventAdmin, self).get_queryset(request)
next_two_months_events_list = qs.filter(event_from__gte=next_two_months, event_from__lte=not_three_months)
rest_events_list = qs.filter(date__lte=datetime.today()).order_by('event_from')
return next_two_months_events_list | rest_events_list
这是我的测试代码:
def test_queryset_events_list(self):
self.client.login(**self._get_superuser_data())
factories.EventFactory.create(title='Event title 1', date=date(2018, 7, 24),
event_from=datetime(2018, 8, 22, 10, 0, 0, 0),
event_to=datetime(2018, 8, 24, 10, 0, 0, 0))
factories.EventFactory.create(title='Event title 2', date=date(2018, 7, 24),
event_from=datetime(2019, 7, 24, 10, 0, 0, 0),
event_to=datetime(2019, 7, 29, 10, 0, 0, 0))
factories.EventFactory.create(title='Event title 3', date=date(2018, 7, 24),
event_from=datetime(2019, 2, 22, 10, 0, 0, 0),
event_to=datetime(2019, 2, 24, 10, 0, 0, 0))
url = reverse('admin:events_event_changelist')
response = self.client.get(url)
print(models.Event.objects.all()[0])
print(models.Event.objects.all()[1])
print(models.Event.objects.all()[2])
self.assertEquals(models.Event.objects.count(), 3)