python - django admin list display sorting from many to many -
please me in solving issue tried lot unfortunately not find solution
my code under
models.py
class measuredcontroller(measuredobject): ..... tempraturemeasure = models.manytomanyfield(measuredtemperature, blank=true, null=true) ..... class measuredtemperature(measuredobject): ...... temperature = models.decimalfield(max_digits=6,decimal_places=1,verbose_name='temperature[c]') .....
admin.py
class measuredcontrolleradmin( admin.modeladmin): list_display = ('tem0', 'tem1', 'tem2') def tem2(self, obj): ............... return obj.tempraturemeasure.filter(deviceindex="2").latest("senddate").temperature ....... def tem1(self, obj): ............... return obj.tempraturemeasure.filter(deviceindex="1").latest("senddate").temperature ....... def tem0(self, obj): ............... return obj.tempraturemeasure.filter(deviceindex="0").latest("senddate").temperature .......
my question possible make sorting on tem0
, tem1
, tem2
column in admin
you can creating sub queries, try this
class measuredcontrolleradmin( admin.modeladmin): list_display = ('tem0', 'tem1', 'tem2') def tem2(self, obj): ............... return obj.tempraturemeasure.filter(deviceindex="2").latest("senddate").temperature ....... def tem1(self, obj): ............... return obj.tempraturemeasure.filter(deviceindex="1").latest("senddate").temperature ....... def tem0(self, obj): ............... return obj.tempraturemeasure.filter(deviceindex="0").latest("senddate").temperature ....... def get_queryset(self, request): qs = super(measuredcontrolleradmin, self).get_queryset(request) qs = qs.extra( select = { "tem2" : "<sub query selecting latest temp2>"), "tem1" : "<sub query selecting latest temp1>"), sub query query selecting latest temp1"), "temp0:"<sub query selecting latest temp0>")}) tem2.admin_order_field = 'tem2' tem1.admin_order_field = 'tem1' tem0.admin_order_field = 'tem0'
Comments
Post a Comment