[Django]-Django model formset factory and forms

6👍

let me explain the way you can do it.

MODELS

from django.utils.text import slugify
from django.db import models
from custom_user.models import AbstractEmailUser

# User model
class UserModel(AbstractEmailUser):
    full_name = models.CharField(max_length=255)

    def __str__(self):
        return str(self.id)

# Function for getting images from instance of user
def get_image_filename(instance, filename):
    title = instance.id
    slug = slugify(title)
    return "user_images/%s-%s" % (slug, filename)

# Save images with user instance
class UserImages(models.Model):
    user = models.ForeignKey('UserModel', db_index=True, default=None)
    image = models.ImageField(upload_to=get_image_filename, verbose_name='Image', db_index=True, blank=True, null=True)

In forms it’s a just a two form, one for model User, other for UserImages model.

# Images forms
class ImageForm(forms.ModelForm):
    image = forms.ImageField(label='Image', required=False)

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

# User form
class UserForm(forms.ModelForm):
    full_name = forms.CharField(required=True)

    class Meta:
        model = UserModel
        fields = ('full_name','email','password',)

And in Views for post you can do something like this

# View
from models import *
from forms import *

@csrf_protect
def post_view(request):
    template = 'some_template.html'
    ImageFormSet = modelformset_factory(UserImages, form=ImageForm, extra=15)
    if request.method == 'POST':
        user_form = UserForm(request.POST, prefix='form1')
        formset = ImageFormSet(request.POST, request.FILES, queryset=UserImages.objects.none(), prefix='form2')

        if user_form.is_valid() and formset.is_valid():
            # Save User form, and get user ID
            a = user_form.save(commit=False)
            a.save()

            images = formset.save(commit=False)
            for image in images:
                image.user = a
                image.save()

            return HttpResponseRedirect('/success/')
        else:
            user_form = UserForm(prefix='form1')
            formset = ImageFormSet(queryset=UserImages.objects.none(), prefix='form2')
    return render(request, template, {'form_user':user_form,'formset':formset})

In template you are doing the right thing.

👤Marin

Leave a comment