预期的预期字符串或类似字节的对象

时间:2019-04-25 10:18:56

标签: python django

我的django应用程序出现问题,尽管我看不出问题所在,但它似乎更多是python问题

这是我的代码

 for q in qs:

        untouched_question_in_term_of_minutes = (now() - q.date) #take the current date 
        #and substract the date when the question was created
        certain_mn_ago = untouched_question_in_term_of_minutes.total_seconds() / 60 
        #that gives me the number of minutes where 
        #the question has not been touched
        limit_of_mn = 50
        print(untouched_question_in_term_of_minutes)
        print(certain_mn_ago)

        if certain_mn_ago >= limit_of_mn: 
        #if the  condition is fulfilled, then the action below are done
            ae = AssociatedExpert.objects.filter(question=q, state='P')
            ae.update(state='C')
            Question.objects.filter(id=q.id, state='P').update(state='C')

            qs.filter(date__lte=certain_mn_ago, state='C').update(email='***',
                                             first_name='***',
                                             last_name='***',
                                             phone='***',
                                             extra='***')
        else:
            ae = AssociatedExpert.objects.filter(question=q,
                                                 state__in=['D', 'T', 'A',
                                                            'F']).first()
            if ae:
                qs.filter(id=q.id).update(state=ae.state)

如您所见,这里的逻辑似乎很混乱。但是,它给了我下面的追溯。

TypeError at /temp_app/question/
expected string or bytes-like object
Request Method: GET
Request URL:    http://127.0.0.1:8000/temp_app/question/
Django Version: 2.0.3
Exception Type: TypeError
Exception Value:    
expected string or bytes-like object
Exception Location: 
/home/andykw/cloned_projects/findoor-backend/.venv/lib/python3.6/site-packages/django/utils/dateparse.py in parse_datetime, line 107
Python Executable:  /home/andykw/cloned_projects/findoor-backend/.venv/bin/python
Python Version: 3.6.7
Python Path:    
['/home/andykw/cloned_projects/findoor-backend/findoor_backend',
 '/home/andykw/cloned_projects/findoor-backend/.venv/lib/python36.zip',
 '/home/andykw/cloned_projects/findoor-backend/.venv/lib/python3.6',
 '/home/andykw/cloned_projects/findoor-backend/.venv/lib/python3.6/lib-dynload',
 '/usr/lib/python3.6',
 '',
 '/home/andykw/cloned_projects/findoor-backend/.venv/lib/python3.6/site-packages',
 '/home/andykw/cloned_projects/findoor-backend/.venv/src/django-s3-upload',
 '/home/andykw/cloned_projects/findoor-backend/.venv/lib/python3.6/site-packages/IPython/extensions',
 '/home/andykw/.ipython',
 '/home/andykw/cloned_projects/findoor-backend/.venv/lib/python3.6/site-packages/odf',
 '/home/andykw/cloned_projects/findoor-backend/.venv/lib/python3.6/site-packages/odf',
 '/home/andykw/cloned_projects/findoor-backend/.venv/lib/python3.6/site-packages/odf',
 '/home/andykw/cloned_projects/findoor-backend/.venv/lib/python3.6/site-packages/odf',
 '/home/andykw/cloned_projects/findoor-backend/.venv/lib/python3.6/site-packages/odf',
 '/home/andykw/cloned_projects/findoor-backend/.venv/lib/python3.6/site-packages/odf',
 '/home/andykw/cloned_projects/findoor-backend/.venv/lib/python3.6/site-packages/odf']
Server time:    Thu, 25 Apr 2019 11:49:27 +0200

我曾考虑过将untouched_question_in_term_of_minutescertain_mn_ago转换为int,但问题仍然存在。

有趣的是,当我使用ipdb时,一切似乎都没问题。

如果您有任何想法,我会全力以赴。

更新:我已经找到了问题。 date__lte部分就是造成问题的原因。

1 个答案:

答案 0 :(得分:1)

在这一行中,由于除法,变量certain_mn_agofloat

certain_mn_ago = untouched_question_in_term_of_minutes.total_seconds() / 60 

然后,您可以使用该float值来过滤名为date的字段(我认为这是某种DateField):

qs.filter(date__lte=certain_mn_ago, ...

这很可能是导致错误的原因,因为您无法将floatDateField进行比较。