1

I have a Django view created with django-datatables-view:

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

With the entity for loop in prepare_results(), I am able to show the list of entities in the DataTable. However, I am running into Related Field got invalid lookup: errors with tracebacks like this one:

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]))

I'm guessing I need to override filter_queryset(), but I'm not sure where to begin. I understand that filter_queryset() is using columns to then filter the underlying column. This bypasses the data customization in prepare_results(). Here are my models:

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')

How should I adjust the view to properly reference the M2M field? Ideally, I would also get the string representation __str__ of Entity rather than just the entityDescription field.

OverflowingTheGlass
  • 2,324
  • 1
  • 27
  • 75

0 Answers0