Reputation: 85
Good day. I have a Django views that searches items in the database. The user can search with email or order number while filtering the database it filters for date. If I try to search I get these as error
[u"'' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]
Here is my views.py
def search_form(request):
if request.method == 'GET':
user_search = request.GET.get('order')
print "What is searched for is : ", user_search
start_date = request.GET.get('first_date')
print "The start date is : ", start_date
end_date = request.GET.get('second_date')
print "The end date is : ", end_date
if user_search != None or start_date != None or end_date != None:
items = Order.objects.filter(Q(order_number__iexact=user_search) |
Q(client__email__iexact=user_search) |
Q(created_on__range=[start_date, end_date])
)
print "items ", items
The error is pointing to Q(created_on__range=[start_date, end_date])
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "C:\Users\Uchechukwu\Dropbox\Engrs-Shared-Projects\JSA_WEB\jsa_admin\views.py" in search_form
926. Q(created_on__range=[start_date, end_date])
File "C:\Python27\lib\site-packages\django\db\models\manager.py" in manager_method
122. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py" in filter
790. return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
808. clone.query.add_q(Q(*args, **kwargs))
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py" in add_q
1243. clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py" in _add_q
1263. current_negated, allow_joins, split_subq)
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py" in _add_q
1269. allow_joins=allow_joins, split_subq=split_subq,
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py" in build_filter
1203. condition = self.build_lookup(lookups, col, value)
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py" in build_lookup
1099. return final_lookup(lhs, rhs)
File "C:\Python27\lib\site-packages\django\db\models\lookups.py" in __init__
19. self.rhs = self.get_prep_lookup()
File "C:\Python27\lib\site-packages\django\db\models\lookups.py" in get_prep_lookup
57. return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_lookup
746. return [self.get_prep_value(v) for v in value]
File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_value
1440. value = super(DateTimeField, self).get_prep_value(value)
File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_value
1296. return self.to_python(value)
File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py" in to_python
1423. params={'value': value},
Exception Type: ValidationError at /dashboard/search_form/
Exception Value: [u"'' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]
Upvotes: 2
Views: 28208
Reputation: 3639
use this dateformat
import datetime
datetime.datetime.now().strftime("%Y-%d-%m %H:%M:%S")
Upvotes: -1
Reputation: 1
Had come across the same issue, and simply solved it by:
date = obj.date
time = obj.time
start_date = "%sT%s" % (date, time)
Upvotes: 0
Reputation: 4164
This error is likely to occur because Django expects DateTimeField values in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]
format, and you probably formatted it as a string before, by using strftime
, so that Django is not able to recognize properly which number represent a corresponding datetime variable (year, month, day, hour, minute, second, timezone).
You can solve it by converting your string back to the datetime by using strptime,
start_date = start_date.strptime(date_string, "YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]")
end_date = end_date.strptime(date_string, "YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]")
or you can simply remove the command
start_date.strftime("your_chosen_format")
end_date.strftime("your_chosen_format")
where you used it before.
Upvotes: 0
Reputation: 349
I had a similar issue while deploying the code on heroku.
After correcting my model attribute to
models.DateField(blank=True, null=True,default=datetime.date.today)
I removed all the files in migrations/* and ran the migrations on local and heroku.
python manage.py makemigrations
python manage.py migrate
That helped in fixing migration problems on Heroku.
Upvotes: 2
Reputation: 599600
You checked if any of the parameters are not None. But if either of start or end date are None, that check will pass, but the query will fail with the error you see. You need to check that both start and end date are not None before filtering on them.
Upvotes: 3