Django - Can you use property as the field in an aggregation function?

前端 未结 2 1912
孤城傲影
孤城傲影 2020-12-16 11:33

I know the short answer because I tried it. Is there any way to accomplish this though (even if only on account of a hack)?

class Ticket(models.Model):
    a         


        
相关标签:
2条回答
  • 2020-12-16 11:44

    I have a similar scenario and want exactly the same feature. I solved it trivially with the following line:

    ...
    return sum(lt.cost for lt in self.lineitem_set)
    
    0 讨论(0)
  • 2020-12-16 11:52

    No. Anything that goes through a built-in manager has to be a real field, since they only touch the database. In order to work with a property they'd have to turn every record in the table into a model, then filter through them in Python.

    0 讨论(0)
提交回复
热议问题