django-datatable-view icon indicating copy to clipboard operation
django-datatable-view copied to clipboard

Ordering tree structures

Open doganmeh opened this issue 7 years ago • 2 comments

For a model like this:

class Person(models.Model):
    name = Charfield()
    parent = ForeignKey('self', null=True, blank=True)

I would like to order the DatatableView of this model such that children are by their parents. Therefore, I annotate the queryset to produce a field of parent_id_or_id and order by it. Such queryset is fed to the DatatableView via get_queryset() method.

However, Datatable seems to introspect the model linked to the queryset and ignore the annotated fields. Thus the following gives an error:

ordering=['parent_id_or_id']

even if that field is defined as a column.

Is there a way around this problem?

doganmeh avatar Apr 27 '17 18:04 doganmeh

Theoretically there should not even be need for:

ordering=['parent_id_or_id']

if the queryset given was iterated with the initial order intact, however, that does not seem to be the case.

doganmeh avatar Apr 27 '17 19:04 doganmeh

Yeah, default Model.Meta ordering can mess up a datatable if the named model field isn't actually on the datatable, which is a problem with how the datatable cleans the ordering in the request; it presumes that ordering fields are columns, but that might not be true about the default data set.

I am actively working on making this better, and I'll use annotated fields in my tests. Thanks for the report.

tiliv avatar Apr 27 '17 19:04 tiliv