楷模:class Event(models.Model): place = models.ForeignKey(SomePlacesModel, on_delete = models.CASCADE) event_name = models.CharField('Event Name', max_length = 20) event_date= models.DateField('Event Date') ...class Result(models.Model) event = models.ForeignKey(Event, on_delete = models.CASCADE) event_name = models.CharField('Event Name', max_length = 20) # Cache from Event result = models.FloatField('Result') some1 = .... some2 = ....我需要获取上个月(周等)按 Event.event_date 排序的结果查询集(但这不起作用..):res = Result.objects.select_related('event__event_date').filter(some1 = something1, some2 = something2).order_by('event_name')[range:]谢谢。
1 回答
陪伴而非守候
TA贡献1757条经验 获得超8个赞
这不是.select_related(…)
[Django-doc]应该做的。您可以通过以下方式在相关字段上订购:
res = Result.objects.filter(
some1=something1, some2=something2
).order_by(
'event__event_date'
)
如果您只想检索上周的Resultfor ,您可以使用以下内容进行过滤:Event
from django.utils.timezone import now
from datetime import timedelta
res = Result.objects.filter(
some1=something1, some2=something2,
event__event_date__gte=now().date()-timedelta(days=7)
).order_by(
'event__event_date'
)
添加回答
举报
0/150
提交
取消