I have the following code:
def addbook(request):
form = AuthorForm()
book_formset = BookFormset(instance=Author())
I know that BookFormset
is an inline formset and that Author
is an object.
Why is Author
part of (instance=)
; what does it mean and what does it do?
As the documentation states, the instance
keyword argument is passed the model whose relations that the formset will edit.
If you want to create a formset that allows you to edit books belonging to a particular author, you could do this:
>>> from django.forms.models import inlineformset_factory >>> BookFormSet = inlineformset_factory(Author, Book, fields=('title',)) >>> author = Author.objects.get(name='Mike Royko') >>> formset = BookFormSet(instance=author)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With