有没有办法让这个查询与 Django 一起工作?
stage = Task.objects.filter(id = input_stage.id)\
.update(start_date = max(data.get('start_date'), F('start_date')),
finish_date = max(data.get('finish_date'), F('finish_date')))
现在我得到一个错误:
TypeError: '>' not supported between instances of 'F' and 'datetime.date'
请您参考如下方法:
数据库中两个字段之间的 max 在 Django 中是 Greatest [Django-doc] ,所以查询应该是这样的:
from django.db.models import F, Value
from django.db.models.functions import <b>Greatest</b>
Task.objects.filter(
id=input_stage.id
).update(
start_date=<b>Greatest</b>(Value(data.get('start_date')), F('start_date')),
finish_date=<b>Greatest</b>(Value(data.get('finish_date')), F('finish_date'))
)
您可能还想将 data.get('start_date') 等转换为 date 对象。
请注意 .update(..) [django-doc] 不返回一个QuerySet,但是:
(...) and returns the number of rows matched (which may not be equal to the number of rows updated if some rows already have the new value).
