[Answer]-How to set default field values when Django's reverse() is used for admin "add" page?

1👍

you could do it by passing some values in GET, but this wont work always(e.g. for datefields etc..), i would do it with custom ModelAdmins with get_form method

class MyModelAdmin(admin.ModelAdmin):
    def get_form(self, request, obj=None, **kwargs):
       form = super(MyModelAdmin, self).get_form(request, obj, **kwargs)
       form.base_fields['your_field_name'].initial = 'abcd'
       return form

0👍

did you try simply adding it to a string?

reverse("admin:...")+"&fieldname=value"

you may need to use formating like this

return HttpResponseRedirect('%s%s'%(reverse("..."), '&field=value'))

depending on what your values are, you may need to also use a uri encoding routine (there are many ways to do this).

Leave a comment