【发布时间】:2023-03-30 20:00:01
【问题描述】:
如果不修改管理界面,我不确定这是否可行。
我有一个名为“Quote”的模型,它可以包含多个“Product”模型。我使用中间模型“QuoteIncludes”将两者连接起来。以下是目前的三种模型:
class Product(models.Model):
name = models.CharField(max_length=100)
short_desc = models.CharField(max_length=200)
default_cost = models.DecimalField(max_digits=15, decimal_places=2)
default_price = models.DecimalField(max_digits=15, decimal_places=2)
shipping_per_unit = models.DecimalField(max_digits=9, decimal_places=2)
weight_in_lbs = models.DecimalField(max_digits=5, decimal_places=2)
def __unicode__(self):
return self.name
class Quote(models.Model):
## Human name for easy reference
name = models.CharField(max_length=100)
items = models.ManyToManyField(Product, through='QuoteIncludes')
def __unicode__(self):
return self.name
class QuoteIncludes(models.Model):
## Attach foreign keys between a Quote and Product
product = models.ForeignKey(Product)
quote = models.ForeignKey(Quote)
## Additional fields when adding product to a Quote
quantity = models.PositiveIntegerField()
per_unit_cost = models.DecimalField(max_digits=15, decimal_places=2)
per_unit_price = models.DecimalField(max_digits=15, decimal_places=2)
def _get_extended_price(self):
"""Gets extended price by multiplying quantity and unit price."""
if self.quantity and self.per_unit_price:
return self.quantity * self.per_unit_price
else:
return 0.00
extended_price = _get_extended_price
我想做的是在管理界面中创建一个报价单,这样当我填写了订单项的数量和 per_unit_price 时,它会将“extended_price”作为产品填写当我翻页时,这两个。我认为它需要在其中添加一些 AJAX。
【问题讨论】:
-
无论您使用什么解决方案,如果您不希望用户能够使用自己的价格提交任意值,请注意安全性。
-
嗯,这个视图将用于“引用”产品列表,因此编辑这些数字的人可以根据需要进行调整。感谢您指出这一点。
标签: python django methods django-admin admin