One can easily set the initial value for Django Admin Add Form, via url get parameter. For example: http://localhost:8080/admin/myapp/mytable/?myfield_id=1
This will not work if myfield is within the inline form, instead of regular form. To do that, override admin.TabularInline formfield_for_foreignkey method.
Here is an example:
class MyTabInline(admin.TabularInline): model = models.MyModel extra = 1 def formfield_for_foreignkey(self, db_field, request, **kwargs): if db_field.name == 'myfield': kwargs['initial'] = request.GET.get('myfield_id', '') return super(MyTabInline, self). formfield_for_foreignkey(db_field, request, **kwargs)Pretty simple.
Just searched something like that :)
ReplyDeleteThanks a lot!
Have you also tried this on ModelForms that declare additional fields?>
ReplyDelete@solvik: You're welcome.
ReplyDelete@Izz ad-Din: No, I have not. I am curious what exactly do you want to achieve. Perhaps you could elaborate more.