I can't figure out what's wrong with my editing view.

I have a view that is for editing products models that also have multiple images via a foreign key. I get this using a modelformset_factory The one for adding them works perfectly. But the one for updating them submits with no errors on the form or in the server console, reloads the form, and when I got to the product page nothing has been updated.

Here is my code

models.py

from django.db import models
from django.template.defaultfilters import slugify

from users.models import User

# Create your models here.

def get_thumbnail_filename(instance, filename):
    title = 
    slug = slugify(title)
    return "post_images/%s-%s" % (slug, filename)  



class Product(models.Model):
    seller = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    name = models.CharField(max_length=200)
    description = models.TextField()
    created = models.DateTimeField(auto_now_add=True)
    price = models.FloatField()
    thumbnail = models.ImageField(upload_to=get_thumbnail_filename, null=True)
    
    def __str__(self):
        return 



def get_image_filename(instance, filename):
    title = 
    slug = slugify(title)
    return "post_images/%s-%s" % (slug, filename)  



class Images(models.Model):
    product = models.ForeignKey(Product, default=None, on_delete=models.CASCADE, null=True)
    img = models.ImageField(upload_to=get_image_filename, verbose_name='Image', blank=True)
    class Meta:
        verbose_name_plural = "Images"instance.nameself.nameinstance.product.name

forms.py

from django import forms
from .models import Product, Images

class ProductForm(forms.ModelForm):
    name = forms.CharField(max_length=128)
    description = forms.Textarea()
    price = forms.FloatField()
    thumbnail = forms.ImageField(label='Thumbnail')
 
    class Meta:
        model = Product
        fields = ('name', 'description', 'price', 'thumbnail', )
 
 
class ImageForm(forms.ModelForm):
    #    img = forms.ImageField(label='Image', required=False)    
    class Meta:
        model = Images
        fields = ('img', )

views.py

# My imports
from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse
from django.forms import modelformset_factory
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.http import HttpResponseRedirect

from .forms import ImageForm, ProductForm
from users.models import User
from .models import Product, Images

# The view I'm having trouble with
def edit_product(request, pk):
    product = get_object_or_404(Product, id=pk)
    ImageFormSet = modelformset_factory(Images, form=ImageForm, extra=20, max_num=20)

    if request.method == 'GET':
        postForm = ProductForm(instance=product)
        formset = ImageFormSet(queryset=Images.objects.filter(product=product))  # Filter existing imagesreturn
        return render(request, 'core/product-form.html', {'postForm': postForm, 'formset': formset})
    elif request.method == 'POST':
        postForm = ProductForm(request.POST or None, request.FILES or None, instance=product)
        formset = ImageFormSet(request.POST or None, request.FILES or None, queryset=Images.objects.filter(product=product))

        if postForm.is_valid() and formset.is_valid():
            # Save product form data
            postForm.save()
            for form in formset.cleaned_data:
                if form:
                    image = form['img']
                    photo = Images(product=product, img=image)
                    photo.save()
        else:
            postForm = ProductForm(instance=product)
            formset = ImageFormSet(queryset=Images.objects.filter(product=product))
            return render(request, 'core/product-form.html', {'postForm': postForm, 'formset': formset})

No clue what I’m doing wrong and Gemini can’t figure it out either.

Any help is greatly appreciated!

Welcome @DarthRevan501st !

You don’t show your template, or how you may (or may not) be rendering the errors from the formset, but the symptoms you describe are consistent with an error in what’s being submitted in the formset.

I’d suggest as perhaps the easiest diagnostic would be to print the errors for both the form and the formset in the else clause of your view.

That has helped a lot, Thank you!
I’m now getting “[{‘id’: [‘This field is required.’]}, {‘id’: [‘This field is required.’]}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]” in the server console. So I actually have something to work with. :sweat_smile:
Hopefully I can fix it now

Turns out the problem was my template and that I was using a for loop to render fields instead of just {{ formset }}