I am able to prefill a form using query-string parameters in Django Admin.
Let's say I have the following models:
class Book(models.Model):
id = models.Autofield(primary_key=True)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
name = models.Charfield(max_length=200)
class Author(models.Model):
id = models.Autofield(primary_key=True)
name = models.Charfield(max_length=200)
If I go to /admin/library/author/add/?name=J.+K.+Rowling the author's name will be properly prefilled.
However if I add InlineForms like that:
class BookInline(StackedInline):
model = Book
extra = 0
class AuthorAdmin(ModelAdmin):
inlines = [BookInline]
admin.site.register(Author, AuthorAdmin)
I don't seem to be able to prefill books.
I tried: /admin/library/author/add/?name=J.+K.+Rowling&books-TOTAL_FORMS=1&books-0-name=Harry+Potter+and+the+Philosopher's+Stone
The author form is prefilled, but the first book form is not prefilled. Do you know how one manages that?
If you override
get_formset_kwargs, you can prefill forms with some initial values:However, all extra form will contain the same values.
If you know how we can manage to use the index there, it would improve this answer.