2014-05-11 67 views
2

當對我的記錄進行排序時,我發現如果在模型的datetime字段中有日期,搜索就會崩潰。我需要將該date_produced字段保存到會話中,以便稍後可以將其提取到csv導出。我不知道在哪個過程中,我可以將字段的值轉換爲字符串。Django將datetime轉換爲JSON可串行化字符串

的錯誤是: 「datetime.date(2014,5,6)不JSON序列化」

這是我的views.py:

def sort(request, project_id=1): 
    thisuser = request.user 
    if Project.objects.filter(id=project_id).exists(): 
     project = Project.objects.filter(id=project_id).order_by('pubdate')[0] 
    else: 
     project = None 

    if Project.objects.filter(Q(created_by=thisuser) | Q(project_access__access_list=thisuser), id=project_id).exists(): 
     permission = 1 
    else: 
     permission = None 

    if Asset.objects.filter(project__id=project_id, unique_id=1): 
     assets = 1 
    else: 
     assets = None 


    if request.POST: 
     if Project.objects.filter(Q(created_by=thisuser) | Q(project_access__access_list=thisuser), id=project_id).exists(): 
      if request.POST.get('date_start') and request.POST.get('date_end'): 
       date_start = datetime.strptime(request.POST['date_start'], '%m/%d/%Y') 
       date_end = datetime.strptime(request.POST['date_end'], '%m/%d/%Y') 
       q_date  = Q(date_produced__range=[date_start, date_end]) 
      else: 
       q_date  = Q(date_produced__isnull=False) | Q(date_produced__isnull=True) 

      text_fields = { 
       'asset_type': request.POST.get('asset_type'), 
       'description': request.POST.get('description'), 
       'master_status': request.POST.get('master_status'), 
       'location': request.POST.get('location'), 
       'file_location': request.POST.get('file_location'), 
       'footage_format': request.POST.get('footage_format'), 
       'footage_region': request.POST.get('footage_region'), 
       'footage_type': request.POST.get('footage_type'), 
       'footage_fps': request.POST.get('footage_fps'), 
       'footage_url': request.POST.get('footage_url'), 
       'stills_credit': request.POST.get('stills_credit'), 
       'stills_url': request.POST.get('stills_url'), 
       'music_format': request.POST.get('music_format'), 
       'music_credit': request.POST.get('music_credit'), 
       'music_url': request.POST.get('music_url'), 
       'license_type': request.POST.get('license_type'), 
       'source': request.POST.get('source'), 
       'source_contact': request.POST.get('source_contact'), 
       'source_email': request.POST.get('source_email'), 
       'source_id': request.POST.get('source_id'), 
       'source_phone': request.POST.get('source_phone'), 
       'source_fax': request.POST.get('source_fax'), 
       'source_address': request.POST.get('source_address'), 
       'credit_language': request.POST.get('source_language'), 
       'cost': request.POST.get('cost'), 
       'cost_model': request.POST.get('cost_model'), 
       'total_cost': request.POST.get('total_cost'), 
       'notes': request.POST.get('notes') 
       } 

      boolean_fields = { 
       'used_in_film': request.POST.get('used_in_film'), 
       'footage_blackandwhite': request.POST.get('footage_blackandwhite'), 
       'footage_color': request.POST.get('footage_color'), 
       'footage_sepia': request.POST.get('footage_sepia'), 
       'stills_blackandwhite': request.POST.get('stills_blackandwhite'), 
       'stills_color': request.POST.get('stills_color'), 
       'stills_sepia': request.POST.get('stills_sepia'), 
       'license_obtained': request.POST.get('license_obtained') 
       } 

      q_objects = Q() 

      for field, value in text_fields.iteritems(): 
       if value: 
        q_objects = Q(**{field+'__contains': value}) 

      q_boolean = Q() 

      for field, value in boolean_fields.iteritems(): 
       if value: 
        q_boolean |= Q(**{field: True}) 

      query_results = Asset.objects.values('unique_id', 'asset_type', 'description', 'used_in_film', 'master_status', 
       str('date_produced'), 'location', 'file_location', 'footage', 'footage_format', 'footage_region', 'footage_type', 
       'footage_fps', 'footage_url', 'footage_blackandwhite', 'footage_color', 'footage_sepia', 'stills', 'stills_credit', 
       'stills_url', 'stills_blackandwhite', 'stills_color', 'stills_sepia', 'music', 'music_format', 'music_credit', 
       'music_url', 'license_obtained', 'license_type', 'source', 'source_contact', 'source_email', 'source_id', 
       'source_phone', 'source_fax', 'source_address', 'credit_language', 'total_use', 'timecode_total', 'windowdub_total', 
       'cost', 'cost_model', 'total_cost', 'notes').filter(q_date, q_objects, q_boolean, project__id=project_id) 

for instance in query_results: 
       instance['date_produced'] = instance['date_produced'].strftime('%Y-%m-%d') 

      request.session['query_results'] = list(query_results) 

      args = {'query_results': query_results, 'thisuser': thisuser, 'project': project, 'assets': assets, 'permission': permission} 
      args.update(csrf(request)) 

      args['query_results'] = query_results 

      return render_to_response('sort_results.html', args) 
     else: 
      return render_to_response('sort_results.html', args) 

    else: 

     args = {'thisuser': thisuser, 'project': project, 'assets': assets, 'permission': permission} 
     args.update(csrf(request)) 

     return render_to_response('sort.html', args) 

我有一個STR( 'date_produced' )在Asset.objects.values(str('date_produced'))。filter函數中,但似乎沒有把它變成一個字符串。我也在我的query_results上運行一個列表,以便使該JSON可序列化,但這也不是。

任何想法?

完全回溯:

Traceback (most recent call last): 
    File "/webapps/filmeditdb/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 201, in get_response 
    response = middleware_method(request, response) 
    File "/webapps/filmeditdb/local/lib/python2.7/site-packages/django/contrib/sessions/middleware.py", line 38, in process_response 
    request.session.save() 
    File "/webapps/filmeditdb/local/lib/python2.7/site-packages/django/contrib/sessions/backends/db.py", line 57, in save 
    session_data=self.encode(self._get_session(no_load=must_create)), 
    File "/webapps/filmeditdb/local/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py", line 87, in encode 
    serialized = self.serializer().dumps(session_dict) 
    File "/webapps/filmeditdb/local/lib/python2.7/site-packages/django/core/signing.py", line 88, in dumps 
    return json.dumps(obj, separators=(',', ':')).encode('latin-1') 
    File "/usr/lib/python2.7/json/__init__.py", line 238, in dumps 
    **kw).encode(obj) 
    File "/usr/lib/python2.7/json/encoder.py", line 201, in encode 
    chunks = self.iterencode(o, _one_shot=True) 
    File "/usr/lib/python2.7/json/encoder.py", line 264, in iterencode 
    return _iterencode(o, 0) 
    File "/usr/lib/python2.7/json/encoder.py", line 178, in default 
    raise TypeError(repr(o) + " is not JSON serializable") 
TypeError: datetime.date(2014, 5, 6) is not JSON serializable 

而且它固定的,我需要的代碼工作:

for instance in query_results: 
instance['date_produced'] = instance['date_produced'].strftime('%Y-%m-%d') 

回答

1

你必須之前準備的日期將它傳遞給JSON響應:

json.dump(your_date.strftime('%Y-%m-%dT%H:%M:%S')) 

https://docs.python.org/2/library/time.html#time.strftime

for instance in query_results: 
    instance.date_produced = json.dump(instance.date_produced.strftime('%Y-%m-%dT%H:%M:%S')) 
+0

添加以下行:query_results = json.dumps(query_results.date_produced.strftime('%y-%m-%dT%H:%M:%S'))在query_results過程給我提供屬性錯誤之後: 「'ValuesQuerySet'對象沒有屬性'date_produced'」 - 你有什麼想法可能是什麼?感謝您指點我正確的方向! –

+0

似乎query_results是QuerySet實例。您應該遍歷查詢集中的每個對象以準備它。我會在答案中添加示例。 –

+0

出於某種原因,你的代碼給我的錯誤,'字典'對象沒有'date_produced'屬性,這真是令人困惑,因爲我可以進入它發送的變量和'date_produced'在實例和query_results中,但它是不在字典中。 –