Andy K
Andy K

Reputation: 5044

Expecting expected string or bytes-like object

I'm having an issue on my django app however, it seems to be more of a python issue, although I don't see where the issue lies

This is my code

 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)

As you see, the logic seems to bere here. Yet, it is giving me the traceback below.

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

I thought about using to cast untouched_question_in_term_of_minutes and certain_mn_ago as int but the issue is still here.

The funny part is when I'm using the ipdb, everything seems to be ok.

If you have any ideas, I'm all ear.

update : I have found the issue. It is this date__lte part that is creating the problem.

Upvotes: 0

Views: 366

Answers (1)

Ralf
Ralf

Reputation: 16485

In this line, the variable certain_mn_ago is a float because of the division.

certain_mn_ago = untouched_question_in_term_of_minutes.total_seconds() / 60 

Then you use that float value to filter a field called date (which I would assume is some kind of DateField):

qs.filter(date__lte=certain_mn_ago, ...

This is most likely the cause of your error, because you cannot compare a float to a DateField.

Upvotes: 1

Related Questions