我有一个使用django-datatables-view创建的Django视图:
class ActivityListJson(BaseDatatableView):
columns = ['description', 'entities']
max_display_length = 500
def get_initial_queryset(self):
return Activity.objects.all().prefetch_related('entities')
def prepare_results(self, qs):
data = []
for item in qs:
entities = []
for entity in item.entities.all():
entities.append(entity.entityDescription)
entities = ''.join(entities)
data.append([item.description,
entities])
return data
有了prepare_results()
中的for循环实体,我可以显示DataTable中的实体列表。但是,我遇到这样的回溯,出现了Related Field got invalid lookup:
错误:
Traceback:
File "D:\Python\Anaconda3\envs\django\lib\site-packages\django\core\handlers\exception.py" in inner
41. response = get_response(request)
File "D:\Python\Anaconda3\envs\django\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "D:\Python\Anaconda3\envs\django\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\Python\Anaconda3\envs\django\lib\site-packages\django\views\generic\base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "D:\Python\Anaconda3\envs\django\lib\site-packages\django\views\generic\base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "D:\Python\Anaconda3\envs\django\lib\site-packages\django_datatables_view\mixins.py" in get
51. func_val = self.get_context_data(**kwargs)
File "D:\Python\Anaconda3\envs\django\lib\site-packages\django_datatables_view\base_datatable_view.py" in get_context_data
314. return self.handle_exception(e)
File "D:\Python\Anaconda3\envs\django\lib\site-packages\django_datatables_view\base_datatable_view.py" in handle_exception
265. raise e
File "D:\Python\Anaconda3\envs\django\lib\site-packages\django_datatables_view\base_datatable_view.py" in get_context_data
284. qs = self.filter_queryset(qs)
File "D:\Python\Anaconda3\envs\django\lib\site-packages\django_datatables_view\base_datatable_view.py" in filter_queryset
254. qs = qs.filter(q)
File "D:\Python\Anaconda3\envs\django\lib\site-packages\django\db\models\query.py" in filter
784. return self._filter_or_exclude(False, *args, **kwargs)
File "D:\Python\Anaconda3\envs\django\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
802. clone.query.add_q(Q(*args, **kwargs))
File "D:\Python\Anaconda3\envs\django\lib\site-packages\django\db\models\sql\query.py" in add_q
1250. clause, _ = self._add_q(q_object, self.used_aliases)
File "D:\Python\Anaconda3\envs\django\lib\site-packages\django\db\models\sql\query.py" in _add_q
1270. current_negated, allow_joins, split_subq)
File "D:\Python\Anaconda3\envs\django\lib\site-packages\django\db\models\sql\query.py" in _add_q
1276. allow_joins=allow_joins, split_subq=split_subq,
File "D:\Python\Anaconda3\envs\django\lib\site-packages\django\db\models\sql\query.py" in build_filter
1201. raise FieldError('Related Field got invalid lookup: {}'.format(lookups[0]))
我猜我需要覆盖filter_queryset()
,但是我不确定从哪里开始。我知道filter_queryset()
正在使用columns
来过滤基础列。这将绕过prepare_results()
中的数据定制。这是我的模特:
class Entity(models.Model):
entity = models.CharField(primary_key=True, max_length=25)
entityDescription = models.CharField(max_length=400)
def __str__(self):
"""
Returns default string representation of Entity.
"""
return '%s - %s - %s' % (self.entity,
self.entityDescription)
class Activity(models.Model):
description = models.CharField(max_length=500)
entities = models.ManyToManyField(Entity, related_name='entities')
如何调整视图以正确引用M2M字段?理想情况下,我还将获得__str__
的字符串表示形式Entity
,而不仅仅是entityDescription
字段。