momo2k
unread,Jun 27, 2011, 5:03:57 PM6/27/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Django users
Hello,
Is there a way to set dynamic default values for custom fields in the
admin?
Description of the problem:
# models.py
# there are two models
class Meal(models.Model):
name = ...
def check_new_price(self, price):
# checks if the price is new and creates a new price if
neccessary
class Price(models.Model):
meal = models.ForeignKey(Meal, ....)
valid_to = models.DateField(....) # None for current price
amount = CurrencyField() # nearly identical to IntegerField
# admin.py
# I extended the Admin with a custom form:
class MealModelForm(forms.ModelForm):
price = CurrencyFormField() # Nearly identical to Decimalfield
class Meta:
model = Meal
class MealAdmin(admin.ModelAdmin):
form = MealModelForm
....
def save_model(self, request, obj, form, change):
super(MealAdmin, self).save_model(request, obj, form, change)
obj.check_new_price(form.cleaned_data['price'])
My question is: How do i tell the django admin to display the current
price in the form field? I already tried to add an "price"-attribute
in the ModelAdmin.get_object() method, but that doesn't work. If I
call the Admin page for a meal, the price is blank instead of
displaying the current price.
So, repeating my first question: Is there a way to set dynamic default
values for custom fields in the admin?