From what i understand from the lib and django doc it willl be complicated to do this kind os sorting with pagination and everything : python - Ordering Django queryset by a @property - Stack Overflow
This function seems to be the one to change if we want to be able to do it :
def enhance_queryset(self, queryset, Model, params, request, apply_pagination=True):
# scopes + filter + search
queryset = self.filter_queryset(queryset, Model, params, request)
# sort
if 'sort' in params:
queryset = queryset.order_by(params['sort'].replace('.', '__'))
# segment
if 'segment' in params:
collection = Collection._registry[Model._meta.db_table]
segment = next((x for x in collection.segments if x['name'] == params['segment']), None)
if segment is not None and 'where' in segment:
queryset = queryset.filter(segment['where']())
# limit fields
queryset = self.handle_limit_fields(params, Model, queryset)
# pagination
if apply_pagination:
queryset = self.get_pagination(params, queryset)
return queryset
To have something working like i want i will have to do something like this :
class Cart(models.Model):
id = models.AutoField(primary_key=True)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.PositiveIntegerField(null=False,blank=False)
total = models.FloatField(null=False,blank=False)
def save(self, *args, **kwargs):
self.total = self.quantity * self.product.price
super(Cart, self).save(*args, **kwargs)
The use of smart field has less value in this case