Django:根据其他字段更新字段值
我不确定在不修改管理界面的情况下这是否可能。
我有一个名为“报价”的模型,它可以包含多个“产品”模型。我使用中间模型“QuoteInincludes”将两者连接起来。以下是目前的三个模型:
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
我希望能够在管理界面中创建一个报价,这样当我填写了订单项的数量和单价时,它会填写当我点击时,“extend_price”是两者的乘积。我认为这需要在其中添加一些 AJAX。
I am not sure this is even possible without modifying the Admin interface.
I have a model called "Quote" that can contain multiple "Product" models. I connect the two using an intermediate model "QuoteIncludes". Here are the three models as they currently stand:
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
What I would like to be able to do is create a Quote in the Admin interface such that when I've filled in both the quantity and the per_unit_price of a line item, it fills in the "extended_price" as a product of the two when I tab over. I think it requires adding some AJAX in there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有关如何在模型管理中包含 js 的信息:
http://docs.djangoproject.com/en /dev/ref/contrib/admin/#modeladmin-media-definitions
例如:
您的脚本可能如下所示:
类似的东西。
只是添加了货币格式功能,以防您想使用它。
Info on how to include js in your model admin:
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-media-definitions
For example:
And your script could look something like this:
Something like that.
Just added the currency format function in case you wanted to use it.
您不会轻易将该字段放入更改列表中,因为它属于正在编辑的模型的另一个模型。不过,您可以将 through 模型作为内联包含在该模型下方,然后您可以简单地编写一些 JS,它接受两个输入字段并生成所需的输出值,并将其放入 through 模型上的适当字段中包含在内联中。
或者,编写一个不依赖管理员的自定义视图;o)
You won't easily get that field in to the change list there because it belongs to another model from the one being editied. You wil be able to include the through models as an inline below this model, though, and then you could simply write some JS that takes your two input fields and generates the output value you want and plonks it into the appropriate field on the through model that's included in the inline.
Or, write a custom view that doesn't lean on the admin ;o)