最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Django set field values dynamically based on other fields - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 3

You 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.

发布评论

评论列表(0)

  1. 暂无评论