Django: Order AdminModel by Custom Property -
i have model property i'd have adminmodel ordered by.
class storeadmin(admin.modeladmin): list_display = ('user', 'total',) admin.site.register(store, storeadmin)
model:
class store(models.model): user = models.foreignkey(user, related_name="store") @property def total(self): num = 0 x in coins.objects.all(): num += x.value return num
how order modeladmin total since property? in advance help!
give link shot, may have answer. https://stackoverflow.com/a/8478586/1011998
in short, since property you're trying order doesn't exist in sql, sql has return results , order them property in python instead. can imagine, slower.
i'd suggest looking storing field "total" field in database denormalized field (int) , update whenever user object updated overriding save()
method. way benefit both speed of not having , dynamically recalculate totals on fly (that'll become sql later when working medium-big data), , speed of mysql indexing (always consider "where" , "order by" clauses are).
Comments
Post a Comment