Djange Queryset .annotate-将45天添加到日期字段

时间:2019-08-02 21:31:20

标签: django date django-queryset

我想使用Django .annotate显示TIdMultipartFormDataStream天。

在下面的代码中,

http.Request.ContentType := 'application/octet-stream';
http.Request.Accept := '*/*';
http.Request.AcceptLanguage := 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7';
//http.Request.AcceptEncoding := 'gzip, deflate';
http.Request.Referer := 'https://flagma.ru/my/1538481/podat-obyavlenie.html';

fnShort := ExtractFileName(fn); // fn - full image file
url := 'https://flagma.ru/messageuploadphoto.php?qqfile=' + fnShort;
http.Request.CustomHeaders.Values['X-Mime-Type'] := GetMIMETypeFromFile(fn);
http.Request.CustomHeaders.Values['X-File-Name'] := fnShort;
http.Request.CustomHeaders.Values['X-Requested-With'] := 'XMLHttpRequest';

fs := TFileStream.Create(fn, fmOpenRead or fmShareDenyWrite);
try
  try
    response := http.Post(url, fs);
  except
    response := '';
  end;
finally
  fs.Free;
end;

{ alternatively: you can pass the full file path to Post()...
try
  response := http.Post(url, fn);
except
  response := '';
end;}

工作正常,可以给我加一年,但是我想要日期加上345天。

代码:

my_date + 345

删除代码中的#后,我的页面将不会生成。

四处搜寻之后,我也尝试过:

date_plus_1=ExtractYear('my_date')+1, 

但是那也不起作用。

在.annotate中使用timedelta的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

您忘记指定ExpressionWrapper expression [Django-doc]output_field=...参数:

from datetime import timedelta
from django.db.models import DateTimeField, ExpressionWrapper, F

MyModel.objects.annotate(
    date_plus345=ExpressionWrapper(
        F('creation_date') + timedelta(days=345),
        output_field=DateTimeField()
    )
)