我希望自定义查询管理器功能基于客户提供的百分比率和基于外国汇率模型中货币列的返回率执行。访问仅在“客户模型”中引用的外国“费率”模型列货币值的最佳方法是什么?
models.py:
class Company(models.Model):
name = ...
def __str__(self):
return self.name
class Rate(models.Model):
company = models.ForeignKey(Company)
currency = ...
name = ...
def __str__(self):
return self.name + " " + self.currency
class ClientManager(models.Manager):
def res_currency(self, percentage):
"""
Returns a QuerySet of modified rates annotated with their rates from the given percentage. This can then be filtered.
Usage:
Client.objects.res_currency(percentage).filter(rate__lt=1000).count()
"""
currency = Client.objects.select_related('base_rate').values('base_rate__currency')
Expression = percentage * F(currency)
return self.get_queryset()\
.annotate(rate=Expression)
class Client(models.Model):
name = ...
company = models.ForeignKey(Company)
base_rate = models.ForeignKey(Rate)
objects = ClientManager()
def __str__(self):
return self.name