Making modelformset_factory editing forms

I have been learning django for the last few months, and have been working on an ecommerce project to further my learning, but I’ve been absolutely stumped by an issue I’ve been having recently.

I’m using a ForeignKey to add multiple images to each product, and I currently have a form that allows me to create a post and add up to 20 images to it, I found some help online to make that form work and have studied it a lot to make sure I understand it, but when I tried to make it into an editing form I’ve gotten completely lost.

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 = instance.name
    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 self.name



def get_image_filename(instance, filename):
    title = instance.product.name
    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')
    class Meta:
        verbose_name_plural = "Images"

views.py

@login_required
def add_product(request):
 
    ImageFormSet = modelformset_factory(Images,
                                        form=ImageForm, extra=20)
    #'extra' means the number of photos that you can upload   ^
    if request.method == 'POST':
    
        postForm = ProductForm(request.POST, request.FILES)
        formset = ImageFormSet(request.POST, request.FILES,
                               queryset=Images.objects.none())
    
    
        if postForm.is_valid() and formset.is_valid():
            post_form = postForm.save(commit=False)
            post_form.user = request.user
            post_form.save()
    
            for form in formset.cleaned_data:
                #this helps to not crash if the user   
                #do not upload all the photos
                if form:
                    image = form['img']
                    photo = Images(product=post_form, img=image)
                    photo.save()
            # use django messages framework
            messages.success(request,
                             "Yeeew, check it out on the home page!")
            return HttpResponseRedirect("/")
        else:
            print(postForm.errors, formset.errors)
    else:
        postForm = ProductForm()
        formset = ImageFormSet(queryset=Images.objects.none())
    return render(request, 'core/product-form.html',
                  {'postForm': postForm, 'formset': formset})

This is the part that where I can get a queryset to fill a form, but when I go to save the form reloads blank with no images saying the fields are required

@login_required
def edit_product(request, pk):
    pro_in = get_object_or_404(Product, id=pk) 
    ImageFormSet = modelformset_factory(Images, fields='__all__', extra=20, max_num=20)
            #'extra' means the number of photos that you can upload   ^
    
    if request.method == 'GET':
        context = {'postForm': ProductForm(instance=pro_in), 'formset':ImageFormSet(queryset=Images.objects.filter(product_id=pk))}
        return render(request, 'core/product-form.html', context)

    if request.method == 'POST':
    
        postForm = ProductForm(request.POST, request.FILES, instance=pro_in)
        formset = ImageFormSet(request.POST, request.FILES, queryset=Images.objects.filter(product_id=pk))
    
    
        if postForm.is_valid() and formset.is_valid():
            post_form.save()
            fromset.save()
    
            # use django messages framework
            messages.success(request,
                             "Yeeew, check it out on the home page!")
            return HttpResponseRedirect("/")
        else:
            print(postForm.errors, formset.errors)
    else:
        postForm = ProductForm()
        formset = ImageFormSet(queryset=Images.objects.none())
    return render(request, 'core/product-form.html',
                  {'postForm': postForm, 'formset': formset})

If you need to see my forms.py, and or urls.py just ask.

Any help is greatly appreciated!

New contributor

DuggyWantsYourSoul24 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật