我有一个用 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
使用 for 循环中的实体prepare_results()
,我可以在 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
字段。