Take the 2-minute tour ×

I'm having trouble using the UpdateView for a view consisting of a form and formset.

I have the following models: Item and Picture.

Picture is defined as:

class Picture(models.Model):

    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, blank=False)

    content_type = models.ForeignKey(ContentType, verbose_name="content type",
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey("content_type", "object_id")

I have several models that contain pictures. For example, in the Item model:

class Item(models.Model):

    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, blank=False)
    pictures = generic.GenericRelation(Picture)

I have the following ItemCreateForm:

class ItemCreateForm(ModelForm):

        super(ItemCreateForm, self).__init__(*args, **kwargs)

    class Meta:
        model = Item

The PictureForm:

class PictureForm(forms.ModelForm):

    id  = forms.IntegerField(widget=forms.HiddenInput)

        super(PictureForm, self).__init__(*args, **kwargs)

    def save(self):
         data = self.cleaned_data
         obj = Picture(**data);
         # do something to obj
         # obj.save() 

    class Meta:
        model = Picture
        fields = ['id', 'name']

And the view:

class ItemUpdateView(UpdateView):
    form_class = ItemCreateForm
    template_name = 'item/new.html'
    model = Item
    success_url = '/items/'

    def get_context_data(self, **kwargs):
        context = super(ItemUpdateView, self).get_context_data(**kwargs)
        item = context['object']

        # Dont' create any extra forms when showing an update view
        PictureFormSet = formset_factory(PictureForm, extra=0)

        return {'form': kwargs['form'],
                'picture_formset': UploadFormSet(initial = [ model_to_dict(a) for pic in item.pictures.all()])}


       self.object = self.get_object()

       item_form = ItemCreateForm(request.POST, instance=self.object)

       if item_form.is_valid():
           item = item_form.save(commit=False)

           # How do update the pictures?

This is my urls.py:

url(r'^items/(?P<pk>\d+)/update/$', ItemUpdateView.as_view(), name='item_update')

The template:


{% for field in form %}
    # do something
{% endfor %}

{{ picture_formset.management_form }}
{% for form in picture_formset.forms %}
    # do something
{% endfor %}


I'm new to Django.

The user can dynamically(via jQuery) add/remove pictures through the Picture form in the single template that is used to display the item and multiple pictures.

1 I had to include the id as a hidden field for the picture, otherwise the pictures will be inserted instead of an Update. QN: Is there a better way to do this?

2 How do I update the picture model? Currently request.POST doesn't have all the fields in the model, thus the model is complaining of NULL fields? I'm totally at lost how to deal with formset in an UpdateView and is not the main form, like a simple example of UpdateView with the pk in the url.

PictureFormSet = formset_factory(PictureForm)
picture_formset = PictureFormSet(request.POST, request.FILES)

for picture_form in picture_formset.forms:
share|improve this question
add comment

Your Answer



Browse other questions tagged or ask your own question.