[Answer]-Admin edit view – populate fields by parameter

1πŸ‘

βœ…

This is actually not hard. The add form is instantiated using request.GET to set initial values, so all you need to do is put the appropriate value into your URL:

def add_book_link(self, instance):
    if instance.id:
        url = '%s?publisher=%d' % (reverse('admin:myapp_book_add'), instance.id)
    else:
        url = reverse('admin:myapp_book_add')
    return mark_safe(u'<a href="{u}">Add</a>'.format(u=url))

I’m generally in the habit of setting allow_tags rather than using mark_safe for admin fields.

Leave a comment