在模型.py
class PurchaseOrder(models.Model):
product = models.ManyToManyField('Product', null =True)
total_price= models.FloatField()
def get_total_price(self):
return sum([p.price_for_each_item for p in self.product.all()]) #this just returns a float such as 42323
在 admin.py 中:
search_fields = ['total_price']
将工作。但是,我无法搜索 get_total_price。那么我怎样才能做到这一点,以便搜索可以获取定义。或者我怎样才能使对象 total_price 获取并保存 get_total_price 的回报值?