首页 > 解决方案 > Django 查询错误:int() 参数必须是字符串、类似字节的对象或数字,而不是 'datetime.timedelta'

问题描述

我要记录哪个人的寿命不到 1 个月。

cnt = Personne.objects.annotate(
        duration=Func(Func(F('deces_an_grg'), F('deces_mois_grg'), F('deces_jour_grg'), function='make_date'), Func(F('date_declaration_an_grg'), F('date_declaration_mois_grg'), F('date_declaration_jour_grg'), function='make_date'), function='age')
    ).filter(duration__gt=timedelta(days=30)).count()

但它会发生这样的错误。什么原因?我该如何解决?

int() argument must be a string, a bytes-like object or a number, not 'datetime.timedelta'
Request Method: GET
Request URL:    ....
Django Version: 1.10
Exception Type: TypeError
Exception Value:    
int() argument must be a string, a bytes-like object or a number, not 'datetime.timedelta'
Exception Location: /***/venv/lib/python3.7/site-packages/django/db/models/fields/__init__.py in get_prep_value, line 1832
Python Executable:  /***/venv/bin/python
Python Version: 3.7.1
Python Path:    
['***',
 '/***/venv/lib/python37.zip',
 '/***/venv/lib/python3.7',
 '/***/venv/lib/python3.7/lib-dynload',
 '/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7',
 '/***/venv/lib/python3.7/site-packages']

标签: djangodjango-formsdjango-views

解决方案


推荐阅读