Thanks, My Media_biz is edited inline in another class. So I have: class MetadataInline(admin.StackedInline) and class MetadataAdmin(admin.ModelAdmin)
Can you tell what method I should override and in which class in order to set the initial value while editing inline? Regards, Alex Arshavski. On Feb 5, 11:21 pm, Rajesh Dhawan <rajesh.dha...@gmail.com> wrote: > > > > I have the following classes in my models.py: > > > > > class Default(models.Model): > > > > name = models.CharField(max_length=50, choices = > > > > (('rental_period', 'rental_period'), ('currency', 'currency')), > > > > unique=True) > > > > value = models.CharField(max_length=50) > > > > > class Media_biz(models.Model): > > > > name = models.CharField(max_length=50) > > > > currency = models.ForeignKey(Currency) > > > > rental_period = models.CharField(max_length=5, blank=True, > > > > null=True) > > > > > What I want is: > > > > When I add new Media_biz in my admin page, I want to check if there is > > > > a Default object with the name "currency" or "rental_period" and take > > > > it's value as default. > > > > You want to take its value as default for what field of Media_biz? And > > > if you have two Default instances (one for currency and another for > > > rental_period), which one provides the default value? > > Each Default instance provide default value for one of the fields: > > I want to take the default value of rental_period to rental_period and > > default value of currency to currency. > > The value of the currency should be the name of Currency object for > > foreign key. > > You can override your Media_biz admin model's get_form method and > inject your initial values. It would be something like this: > > def get_form(self, request, obj=None, **kwargs): > form = super(YourMedia_bizAdmin, self).get_form(request, obj, > **kwargs) > try: > currency_default = Default.objects.get(name='currency') > currency = Currency.objects.get(name=currency_default.value) > form.fields['currency'].initial = currency.pk > except Default.DoesNotExist: > pass > try: > rental_default = Default.objects.get(name='rental_period') > form.fields['rental_period'].initial = rental_default.value > except Default.DoesNotExist: > pass > return form > > -RD --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---