python - 如何将 Django 模型计算属性用于 Django 管理器模型?
问题描述
我试图将我的 django 模型计算属性用于自定义管理器以实现自定义结果。但是 django 只允许在管理器中使用普通属性。
我已经尝试将 @property 添加到我的计算属性中,但没有工作。
这是我的代码:
class Produto(models.Model):
nome = models.CharField(max_length=100)
fabricante = models.ForeignKey('Fabricante', on_delete=models.PROTECT)
frete_ipi = models.DecimalField(decimal_places=2, max_digits=10)
price = models.DecimalField(decimal_places=2, max_digits=10)
cost_price = models.DecimalField(decimal_places=2, max_digits=10)
@property
def real_cost_price(self):
p = (self.cost_price * self.frete_ipi) + self.cost_price
return round(p, 2)
而且我正在尝试将“real_cost_price”用于我的经理并且无法正常工作。
class EstoqueCentroManager(models.Manager):
def total_venda(self):
total = self.all().aggregate(valor=Sum(F('quantidade') * F('produto__real_cost_price'), output_field=FloatField()))
return total
我如何将这个计算属性“real_cost_price”用于我的经理?还是有另一种解决方法来实现这种结果?
谢谢!
解决方案
推荐阅读
- python - 即使在我指定它们之后,烧瓶中缺少 3 个必需的位置参数错误
- python - 使用 pandas.DataFrame.assign 时将多个变量传递给函数
- ios - 有什么方法可以在 iOS swift 中支持用于支付卡的 NFC 读取器?
- javascript - 如何获取由 XLSX 模块提取的 excel 文件生成的数组元素而不会未定义
- javascript - 使用自定义浏览器协议启动 nodejs 应用程序
- powershell - 在 powershell core 7.1.3 中安装 pscx 失败
- java - 任务 ':app:installDebug' 执行失败。> com.android.builder.testing.api.DeviceException:com.android.ddmlib.InstallException:EOF
- angular - 运行我的文件更改事件侦听器导致 TypeError:无法在“FileReader”上执行“readAsArrayBuffer”:参数 1 不是“Blob”类型
- git - Git 中的 TAG 名称标准
- git - 发布生成 git 日志输出后,如何从 X、Y、Z 板上找到所有故事编号?并将列表存储在变量中?