izimobil/django-rest-framework-datatables

Dealing with nested relationships

Closed this issue · 5 comments

Hello,

I apologize if this isn't the right place to ask this question, but I am having a rough time trying to get this package working with nested relationships. I have tried using DRFs built in solution, DRF-Flex-Fields, and Dynamic Rest. None of these options seem to be able to expand my sub-serializers and apply the DRF-Datatables formatting appropriately.

Instead, what is happening is that the nested relationships are being stripped away and only the first layer is being presented within the data:[...] layer that is fed to the datatable.

Hello,
Sorry I can't help here, AFAIK Datatables expects a data array and does not know how to deal with nested structures.
At best it can do this but this is not currently supported by drf-datatables.

Hi,

The nested objects were the type of nested structures I was talking about. My API is nested in that fashion.

It seems that drf-datatables strips out nested layers during the _filter_unused_fields() method. I'm going to hack around with it to see if I can get it to filter unused fields while still maintaining the information from columns that have nested data.

I don't have much time to work on this, but the two quick and super-dirty options I've figured out so far are:

  1. disabling _filter_unused_fields() entirely and allowing a more robust API field filtering package to take over (e.g. DRF-Flex-Fields).

  2. adding col.split('.').pop(0) to line 68 of renderers.py as seen in my fork (warning: I haven't written a unit test for this yet). This will search the top level of the data tree and maintain all subsequent serializers. It's quick, it's dirty, and I hate it, but I don't really have time to think of a better way to recursively iterate through the data dict and filtering the appropriate fields at the appropriate level (i.e. recreating the Flex-Fields functionality).

chidg commented

Thanks @neighlyd, the quick and dirty solution works well.

Fixed by #7