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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With