title: How can i update with modelformset_factory on Django
body: <p>I used the modelformset_factory method for multiple photo uploads. But when I want to update the post, I don't know how to do this, I tried something, but this only serves to re-upload photos and upload the uploaded photos again. How can I update or delete a previously uploaded photo? Also, I cannot access the photos in formset as {{formset.image}}. How can I access this?</p>
  <p>Views.py</p>
  <pre><code>def post_update(request, slug):
      post = get_object_or_404(Post, slug=slug)
      if not request.user.is_authenticated and not request.user == post.seller or not request.user.is_admin:
          raise Http404
      ImageFormSet = modelformset_factory(PostImage, form=PostImageForm, extra=5, can_delete = True)
      form = PostForm(data=request.POST or None, instance = post, files = request.FILES or None)
      
      formset = ImageFormSet(data=request.POST or None, files= request.FILES or None, queryset=PostImage.objects.filter(post__slug=slug))
      if form.is_valid():
          if formset.is_valid():
              for forms in formset.cleaned_data:
                  if forms:
                      image = forms['image']
                      print(forms['image'])
                      photo = PostImage(post=post, image=image)
                      photo.save()
          form.save(commit=True)
          messages.success(request,&quot;Success&quot;)
          return HttpResponseRedirect(reverse('gayrimenkul:detail',kwargs={'slug':form.instance.slug}))
      return render(request,'post_update.html',{'form':form,'formset':formset,'slug':slug})
  </code></pre>
  <p>post_update.html</p>
  <pre><code>{% extends &quot;main_page.html&quot; %}
  {% load static %}
  {% block icerik %}
  {% load crispy_forms_tags %}
  &lt;div class=&quot;row&quot;&gt;
  &lt;div class=&quot;container&quot;&gt;
  
      &lt;h2 class=&quot;page_header&quot;&gt;İlan Ver&lt;/h2&gt;
      &lt;hr&gt;
      {% if form.errors %}
          {{form.errors}}
      {% endif %}
      &lt;div class=&quot;col-md-12&quot;&gt;
          &lt;form enctype=&quot;multipart/form-data&quot; method=&quot;POST&quot;&gt;
              {% csrf_token %}
              {{form.media}}
              {{form|crispy}}
              &lt;div class=&quot;row&quot;&gt;
              &lt;div class=&quot;col-md-6&quot;&gt;
              {{formset}}
              &lt;/div&gt;
              &lt;div class=&quot;col-md-6&quot;&gt;
              &lt;button type=&quot;submit&quot; id=&quot;inp&quot; class=&quot;btn btn-outline-primary&quot; style=&quot;float:right;&quot;&gt;Kaydet&lt;/button&gt;
              &lt;/div&gt;
              &lt;/div&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
   {% endblock %}
  </code></pre>
  <p>Creating posts works successfully.</p>
  <pre><code>def post_create(request):
      if not request.user.is_authenticated:
          raise Http404
      ImageFormSet = modelformset_factory(PostImage, form=PostImageForm, extra=5)
      
      if request.method == 'POST':
          post_form = PostForm(request.POST, request.FILES)
          formset = ImageFormSet(request.POST, request.FILES, queryset=PostImage.objects.none())
          if post_form.is_valid():
              created_post = post_form.save(commit=False)
              created_post.seller = request.user
              created_post.save()
              for form in formset.cleaned_data:
                  if form:
                      image = form['image']
                      photo = PostImage(post=created_post, image=image)
                      photo.save()
              messages.success(request,'Success')
              return HttpResponseRedirect(reverse('gayrimenkul:detail',kwargs={'slug':created_post.get_slug()}))
      else:
          post_form = PostForm()
          formset = ImageFormSet(queryset = PostImage.objects.none())
      return render(request,'post_create.html',{'form':post_form,'formset':formset})
  </code></pre>