Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django set field values dynamically based on other fields

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?

like image 799
Tavo Avatar asked Jun 26 '12 15:06

Tavo


2 Answers

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)
like image 130
Mark Ransom Avatar answered Oct 25 '22 11:10

Mark Ransom


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.

like image 23
JCotton Avatar answered Oct 25 '22 09:10

JCotton