[Django]-What is instance= in Django?

4👍

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)

Leave a comment