How to implement multiple file upload in Django with two models? I have 1 form but two models that make two forms

models.py

class Ads(models.Model):
    title = models.CharField(max_length=85, blank=False)

class Images(models.Model):
    ad = models.ForeignKey(Ads, related_name='images', on_delete=models.CASCADE)
    image = models.ImageField(blank=True, upload_to='')
    thumbnail = models.BooleanField(default=False)

views.py

class CreateAd(CreateView):
    model = Ads
    form_class = CreateAdForm
    success_url = reverse_lazy('index')

forms.py

class CreateAdForm(forms.ModelForm):
    class Meta:
        model = Ads
        fields = ('title',)

class ImageForm(forms.ModelForm):

    class Meta:
        model = Images
        fields = ('image', )

1 Answers

0
c.grey On

Basically you are looking for django formset

formset is a layer of abstraction to work with multiple forms on the same page.