简体   繁体   English

错误 AttributeError: 'str' object 在查询 model 时没有属性 'year'

[英]Error AttributeError: 'str' object has no attribute 'year' when querying a model

I get startdate and enddate, but they are passed as a string.我得到 startdate 和 enddate,但它们是作为字符串传递的。 How do I convert them to a date?如何将它们转换为日期?

    startdate = request.POST["startdate"]
    enddate = request.POST["enddate"]
    queryset = paidparking.objects.all()
    qsstats = QuerySetStats(queryset, date_field='expirationdate')
    values = qsstats.time_series(startdate, enddate, interval='days')
    return render(request, 'index/template.html', {'values': values})

Error in: values = qsstats.time_series(startdate, enddate, interval='days')错误:值 = qsstats.time_series(startdate, enddate, interval='days')

startdate and enddate are strings , not datetime objects. startdateenddate字符串,而不是datetime对象。 You need to parse these, so something like:你需要解析这些,所以像:

from datetime import datetime

startdate = datetime.strptime(request.POST['startdate'], '%Y-%m-%d')
enddate = datetime.strptime(request.POST['enddate'], '%Y-%m-%d')
queryset = paidparking.objects.all()
qsstats = QuerySetStats(queryset, date_field='expirationdate')
values = qsstats.time_series(startdate, enddate, interval='days')
return render(request, 'index/template.html', {'values': values})

The format here ( '%Y-%m-%d' ) might be different, depending on how the date is formatted as string.此处的格式 ( '%Y-%m-%d' ) 可能会有所不同,具体取决于日期格式为字符串的方式。

I would however advise to work with a Form [Djang-doc] .但是,我建议使用Form [Djang-doc] These forms make it convenient to parse, clean and validate input.这些 forms 可以方便地解析、清理和验证输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django AttributeError:“ str”对象没有属性“ model” - Django AttributeError: 'str' object has no attribute 'model' AttributeError: 'str' 对象在使用 BeautifulSoup 时没有属性 'descendants' 错误 - AttributeError: 'str' object has no attribute 'descendants' error when using BeautifulSoup AttributeError:“ str”对象在循环时没有属性“ items”错误 - AttributeError: 'str' object has no attribute 'items' Error when Looping AttributeError: 'str' object has no attribute 'shape' 错误 - AttributeError: 'str' object has no attribute 'shape' error AttributeError: 'str' object 没有属性 'member' 错误 - AttributeError: 'str' object has no attribute 'member' Error python错误 - attributeError:'str'对象没有属性 - python error - attributeError: 'str' object has no attribute Django-Import-Export: Import Error - AttributeError: 'str' object 没有属性 'year' - Django-Import-Export: Import Error - AttributeError: 'str' object has no attribute 'year' 诊断 AttributeError: 'str' object has no attribute 'str' 错误时最好的第一步是什么 - What are the best first steps when diagnosing AttributeError: 'str' object has no attribute 'str' error AttributeError'str'对象没有属性 - AttributeError 'str' object has no attribute AttributeError:'str'对象没有属性 - AttributeError: 'str' object has no attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM