简体   繁体   中英

How to sort QuerySet with property field Django

I have a model as

class FooModel(models.Model):
    field_1 = models.IntegerField()
    field_2 = models.IntegerField()

    @property
    def field_sum(self):
        return self.field_1 + self.field_2

How can I sort the queryset with field_sum field?

FooModel.objects.all().order_by('field_sum') is giving me error. Thanks in advance

Use F() expression along with annotate() as below,

from django.db.models.expressions import F

FooModel.objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM