2016-04-04 21 views
1

おはよう。 私はデータベース内のアイテムを検索するDjangoビューを持っています。ユーザーは、電子メールまたは注文番号で検索することができますが、データベースをフィルタリングして日付用にフィルタリングします。 私が検索しようとすると、私はこれらのエラーとして[u "'値の形式が無効です.YYYY-MM-DD HH:MM [:ss [.uuuuuu]] [TZ]形式である必要があります。"]

[U " '' 値が無効な形式を持ってもらうことがYYYY-MM-DD HHにする必要があります。MM [:ssは[.uuuuuu]] [ 。TZ]形式 "]ここで

Q(created_on__range=[start_date, end_date])

トレースバックに私の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 

エラーが指している:

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."] 
+0

実際のトレースバックメッセージを含めてください。空の文字列から日付を作ることを試みているようです。 –

答えて

2

いずれかのパラメータが[なし]であるかどうかを確認しました。しかし、開始日と終了日のいずれかがNoneの場合、そのチェックは合格しますが、表示されるエラーでクエリが失敗します。フィルタリングする前に、開始日と終了日の両方が「なし」でないことを確認する必要があります。

+0

私は日付でデータベースを照会すると動作しますが、日付エラーのないクエリが出てくる場合 – uche

+0

はい。そうしないでください。 –

2

herokuにコードをデプロイする際に同様の問題が発生しました。

models.DateField(blank=True, null=True,default=datetime.date.today)

に私のモデル属性を修正した後、私は* /移行内のすべてのファイルを削除し、ローカルおよびHerokuの上でマイグレーションを実行しました。 Herokuの上の移行問題を修正するに助け

python manage.py makemigrations python manage.py migrate

関連する問題