date
필드와 다음 방법 이있는 모델이 있습니다 .
class Pm(models.Model):
mydate = models.DateField()
@property
def due_date(self):
today = timezone.localtime(timezone.now()).date()
if self.mydate < today:
return today
else:
return self.mydate
기능 myModel.objects.all()
반환을 기준으로 주문하려면 어떻게 due_date
해야합니까? 나는 시도했다 :
qs = sorted(Pm.objects.all(), key=lambda a: a.due_date(), reverse=True)
하지만 오류를 반환합니다.
'datetime.date' object is not callable
업데이트 Sami의 제안에 따라 :
@staticmethod
def due_date(obj):
today = timezone.localtime(timezone.now()).date()
if obj.pm_date < today:
return today
else:
return obj.pm_date
qs = sorted(Pm.objects.all(), key=Pm.due_date, reverse=True)
쿼리 셋 대신 목록을 반환한다는 점을 제외하면 작동하는 것 같습니다.
쿼리 셋을 얻으려면 어떤 방법을 사용해야합니까?
솔직히 파이썬으로 정렬하지 않을 것입니다. 두 가지 이유가 있습니다.
궁극적으로 쿼리 셋을 원하므로 간단하게 유지하십시오.
qs = Pm.objects.order_by('-mydate')
# Now we can display the due date property when we render things:
for pm in qs:
print(pm.due_date)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다