2016-02-05 80 views
1

我有2種型號的Django項目:「ManyToOneRel」對象有沒有屬性「parent_model」 Django的錯誤chartit

class DeviceModel(models.Model): 
    name = models.CharField(max_length=255) 
    def __unicode__(self): 
     return self.name 

class Device(models.Model): 
    created_at = models.DateTimeField(auto_now_add=True) 
    device_model = models.ForeignKey(DeviceModel) 
    serial_number = models.CharField(max_length=255) 

    def __unicode__(self): 
     return self.device_model.name + " - " + self.serial_number 

在數據庫中有許多設備,我要繪製圖表每個「設備的數量」 「設備模型」。

我想用django chartit來完成這個任務。

守則view.py:

ds = PivotDataPool(
    series=[ 
     {'options': { 
      'source':Device.objects.all(), 
      'categories':'device_model' 
      }, 
     'terms':{ 
       u'Amount':Count('device_model'), 
      } 
     } 
    ], 
) 

pvcht = PivotChart(
    datasource=ds, 
    series_options = 
      [{'options':{ 
      'type': 'column', 
      'stacking': True 
      }, 
      'terms':[ 
      u'Amount'] 
      }], 
    chart_options = 
      {'title': { 
       'text': u'Device amount chart'}, 
      'xAxis': { 
       'title': { 
        'text': u'Device name'}}, 
      'yAxis': { 
       'title': { 
        'text': u'Amount'}}}      
) 


return render(request, 'charts.html', {'my_chart': pvcht}) 

這似乎繪製結果我需要的,但不是設備名稱它繪製ForeignKey的(1,2,3,4 ......)的值,我需要實際的設備型號名稱。

我認爲解決辦法是「類別」的值更改爲:

'categories':'device_model__name' 

但是這給了我錯誤:

'ManyToOneRel' object has no attribute 'parent_model' 

這種類型的引用的應該工作accoring官方例子http://chartit.shutupandship.com/demo/pivot/pivot-with-legend/

我在這裏錯過了什麼?


C:\Anaconda\lib\site-packages\django\core\handlers\base.py in get_response 
        response = middleware_method(request, callback, callback_args, callback_kwargs) 
        if response: 
         break 
      if response is None: 
       wrapped_callback = self.make_view_atomic(callback) 
       try: 
           response = wrapped_callback(request, *callback_args, **callback_kwargs) ### 
       except Exception as e: 
        # If the view raised an exception, run it through exception 
        # middleware, and if the exception middleware returns a 
        # response, use that. Otherwise, reraise the exception. 
        for middleware_method in self._exception_middleware: 
         response = middleware_method(request, e) 

D:\django\predator\predator\views.py in charts 
     series=[ 
      {'options': { 
       'source':Device.objects.all(), 
       'categories':'device_model__name' 
       }, 
       #'legend_by': 'device_model__device_class'}, 
      'terms':{ 
           u'Amount':Count('device_model'), ### 
        } 
      } 
     ], 
     #pareto_term = 'Amount' 
    ) 

C:\Anaconda\lib\site-packages\chartit\chartdata.py in __init__ 
      'terms': { 
       'asia_avg_temp': Avg('temperature')}}] 

     # Save user input to a separate dict. Can be used for debugging. 
     self.user_input = locals() 
     self.user_input['series'] = copy.deepcopy(series) 

        self.series = clean_pdps(series) ### 
     self.top_n_term = (top_n_term if top_n_term 
          in self.series.keys() else None) 
     self.top_n = (top_n if (self.top_n_term is not None 
           and isinstance(top_n, int)) else 0) 
     self.pareto_term = (pareto_term if pareto_term in 
          self.series.keys() else None) 

C:\Anaconda\lib\site-packages\chartit\validation.py in clean_pdps 

def clean_pdps(series): 
    """Clean the PivotDataPool series input from the user. 
    """ 
    if isinstance(series, list): 
     series = _convert_pdps_to_dict(series) 
        clean_pdps(series) ### 
    elif isinstance(series, dict): 
     if not series: 
      raise APIInputError("'series' cannot be empty.") 
     for td in series.values(): 
      # td is not a dict 
      if not isinstance(td, dict): 

C:\Anaconda\lib\site-packages\chartit\validation.py in clean_pdps 
      try: 
       _validate_func(td['func']) 
      except KeyError: 
       raise APIInputError("Missing 'func': %s" % td) 
      # categories 
      try: 
       td['categories'], fa_cat = _clean_categories(td['categories'], 
                     td['source']) ### 
      except KeyError: 
       raise APIInputError("Missing 'categories': %s" % td) 
      # legend_by 
      try: 
       td['legend_by'], fa_lgby = _clean_legend_by(td['legend_by'], 
                  td['source']) 

C:\Anaconda\lib\site-packages\chartit\validation.py in _clean_categories 
    else: 
     raise APIInputError("'categories' must be one of the following " 
          "types: basestring, tuple or list. Got %s of " 
          "type %s instead." 
          %(categories, type(categories))) 
    field_aliases = {} 
    for c in categories: 
        field_aliases[c] = _validate_field_lookup_term(source.model, c) ### 
    return categories, field_aliases 
def _clean_legend_by(legend_by, source): 
    if isinstance(legend_by, basestring): 
     legend_by = [legend_by] 
    elif isinstance(legend_by, (tuple, list)): 

C:\Anaconda\lib\site-packages\chartit\validation.py in _validate_field_lookup_term 
     #  and m2m is True for many-to-many relations. 
     # When 'direct' is False, 'field_object' is the corresponding 
     # RelatedObject for this field (since the field doesn't have 
     # an instance associated with it). 
     field_details = model._meta.get_field_by_name(terms[0]) 
     # if the field is direct field 
     if field_details[2]: 
         m = field_details[0].related.parent_model ### 
     else: 
      m = field_details[0].model 

     return _validate_field_lookup_term(m, '__'.join(terms[1:])) 
def _clean_source(source): 
+0

你能顯示堆棧跟蹤嗎? –

+0

也許device__model__name? –

+0

「device__model__name」錯誤。在這種情況下,django查找「設備」字段,它不存在於設備模型中。所以有一個錯誤「字段'設備'不存在」 – kostr22

回答

0

categories你只能使用包含在source查詢集領域。另一方面,您可以使用foreignKey或manyTomany連接的字段。

查找下面的示例。

而不是使用

'source':Device.objects.all() 
'categories':'device_model' 

嘗試使用

'source':DeviceModel.objects.all() 
'categories':'name' 

,旁邊

'Amount':Count('device__device_model') 
0

我覺得這是和Django的新版本(1.8)中的問題。

此代碼已經過時:

m = field_details[0].related.parent_model 

,而不是用它

m = field_details[0].getattr(field.related, 'related_model', field.related.model) 

您還可以找到解決這個問題的GitHub

希望這會有所幫助。

相關問題