admin管理员组文章数量:1335825
I'm trying to set a field default value based on other field selection that is foreign. Basically, these are the classes:
class Product(models.Model):
description = models.CharField('Description', max_length=200)
price = models.FloatField('Price')
class Sell(models.Model):
product = models.ForeignKey(Product)
price = models.FloatField('Price')
Each "Product" has a default price (or suggested price), so when the user, in Admin pages, want to add a new Sell and he/she choose a product, I need to copy dynamically from Product.price to Sell.price the suggested price. I cannot use "save" method because the user can change at that moment.
Is it necessary to use JavaScript explicitly? or Is there an elegant way in Django to do that?
I'm trying to set a field default value based on other field selection that is foreign. Basically, these are the classes:
class Product(models.Model):
description = models.CharField('Description', max_length=200)
price = models.FloatField('Price')
class Sell(models.Model):
product = models.ForeignKey(Product)
price = models.FloatField('Price')
Each "Product" has a default price (or suggested price), so when the user, in Admin pages, want to add a new Sell and he/she choose a product, I need to copy dynamically from Product.price to Sell.price the suggested price. I cannot use "save" method because the user can change at that moment.
Is it necessary to use JavaScript explicitly? or Is there an elegant way in Django to do that?
Share Improve this question asked Jun 26, 2012 at 15:10 TavoTavo 1811 silver badge7 bronze badges2 Answers
Reset to default 3You can tackle this with a pre-save hook or by overriding the save() method of the Sell model.
from django.db import models
class Product(models.Model):
description = models.CharField('Description', max_length=200)
price = models.FloatField('Price')
class Sell(models.Model):
product = models.ForeignKey(Product)
price = models.FloatField('Price')
# One way to do it:
from django.db.models.signals import post_save
def default_subject(sender, instance, using):
instance.price = instance.product.price
pre_save.connect(default_subject, sender=Sell)
# Another way to do it:
def save(self, *args, **kwargs):
self.price = self.product.price
super(Sell, self).save(*args, **kwargs)
Are you asking about dynamically updating form field values in the Admin webpage? Client-side form behavior needs to happen in JavaScript. Django's Admin does provide some scripting particularly for adding/removing Inlines, but it doesn't offer functionality at this level.
Django's Admin does provide jQuery in the page context under window.django.jQuery
(or just django.jQuery
). Django Forms always output stable form field IDs. Examine the output form code and find the Product selector and the corresponding Sell.price input field. Then you can use jQuery to do something like adding a .change() or .on('change', ...) handler to the Product selector with a function that updates the price on the Sell field.
For future reference, Django Admin almost fulfills your request with ModelAdmin.prepopulated_fields but
prepopulated_fields doesn't accept DateTimeField, ForeignKey, nor ManyToManyField fields.
本文标签: javascriptDjango set field values dynamically based on other fieldsStack Overflow
版权声明:本文标题:javascript - Django set field values dynamically based on other fields - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742398685a2467444.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论