2016-02-05 81 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

希望这会有所帮助。

相关问题