0

What is the most straightforward way to edit some fields from a batch of objects using a formset (see below), while displaying along other fields from these models objects?

For example:

I want to display the title of a set of 10 Book objects and a form for each one in which it is possible to edit their ISBN and description.

class BookForm(ModelForm):
    class Meta:
        model = Book
        fields = ('ISBN', 'description')   # The Book model has many more fields

BookFormSet = modelformset_factory(Book, form=BookForm)
Alex Gonzalez
  • 981
  • 1
  • 11
  • 17
  • You could read through http://stackoverflow.com/faq and, hmm, get an extra brozon badget =) – okm Jun 24 '12 at 14:55

1 Answers1

1

According to the doc: a model form instance bound to a model object will contain a self.instance attribute that gives model form methods access to that specific model instance. Thus when you have a formset of Model such as BookFormSet

BookFormSet = modelformset_factory(Book, form=BookForm)
formset = BookFormSet(request.POST, queryset=Book.objects.order_by('-pk')[:10])

You could iterate it in template like:

<ul>{% for f in formset %}
    <li>{% if f.instance.pk %}title: {{ f.instance.title }}{% endif %} {{ f }}</li>
{% endfor %}</ul>
okm
  • 23,575
  • 5
  • 83
  • 90