Related Column Retrieval
I'm trying to figure out how to return related columns (based upon a foreign-key) in the ajax data. The regular django syntax of defining foreignkey__column
does not seem to work. This is how I would expect to specify related columns from the customer
foreign key that I want returned by the query:
columns = [
'key',
'customer__number',
'customer__name',
'customer__phone1'
]
But this doesn't work. It returns nothing in these columns. However I did find an interesting workaround where you can specify foreignkey.column
as the format of related columns
.
columns = [
'key',
'customer.number',
'customer.name',
'customer.phone1'
]
This seems to work but it processes extremely slowly, so I profiled the query and observed very strange behavior where the query was retrieving records one row at a time with highly inefficient queries.
Am I approaching this the wrong way? Was querying related models part of the intended design?
Comments (3)
-
repo owner -
Account Deleted reporter Thanks for the tip! I hadn't dug far enough into your package to start messing with prepare results. I ended overriding that, and using the queryset
values_list
to generate the array, and that seemed to solve the issues that I was having with querying for individual records instead of sets. -
repo owner - changed status to closed
- Log in to comment
The proper syntax is to use dot notation like, 'customer.name'. Internally this just replaces '.' with '__' and does some other stuff, eg. testing for get_OBJECT_display method. This can be seen at: https://bitbucket.org/pigletto/django-datatables-view/src/216fd0db6044d2eef43034f5e905ceff68bdfeaa/django_datatables_view/base_datatable_view.py?at=master&fileviewer=file-view-default#base_datatable_view.py-44
Regarding query speed issues it seems that you have a number of inefficient queries due to related object lookups - you're not using select_related syntax.
Anyway, personally, I tend to override prepare_results method and simply prepare output data here in the most straightforward way.