Django model formset factory and forms
<p>I'm trying to user Django model formset factory to render a template where a user can add images and change the images they have uploaded(very similar to what can be done in the admin). I currently can render the template and its correct fields to the template. What I cannot do is have the user preselected(want currently logged in) and when I refresh the page the image will be posted again(not sure if this is preventable). Below is my code. Thanks!</p>

<p>Model:</p>

<pre><code>class Image(models.Model):
    user = models.ForeignKey(User)
    image = models.ImageField(upload_to=content_file_name, null=True, blank=True)
    link = models.CharField(max_length=256, blank=True)
</code></pre>

<p>Form:</p>

<pre><code>class ImageForm(forms.ModelForm):
    image = forms.ImageField(label='Image')

    class Meta:
        model = Image
        fields = ('image',
                  'link',
                  )
</code></pre>

<p>View:</p>

<pre><code>@login_required
def register(request):

user_data = User.objects.get(id=request.user.id)
ImageFormSet = modelformset_factory(Image,
                                    fields=('user', 'image', 'link'), extra=3)

if request.method == 'POST':
    print '1'
    formset = ImageFormSet(request.POST, request.FILES, queryset=Image.objects.all())

    if formset.is_valid():
        formset.user = request.user
        formset.save()

    return render(request, 'portal/register.html', {'formset': formset, 'user_data': user_data})

else:
    print '2'
    formset = ImageFormSet(queryset=Image.objects.all())
    return render(request, 'portal/register.html', {'formset': formset, 'user_data': user_data})
</code></pre>

<p>Template:</p>

<pre><code>&lt;form id="" method="post" action=""
      enctype="multipart/form-data"&gt;

    {% csrf_token %}

    {{ formset.management_form }}
    {% for form in formset %}
        {{ form }}
    {% endfor %}


&lt;input type="submit" name="submit" value="Submit" /&gt;
</code></pre>

<p></p>

<p>let me explain the way you can do it.</p>

<h1>MODELS</h1>

<pre><code>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)
</code></pre>

<p>In forms it's a just a two form, one for model User, other for UserImages model.</p>

<pre><code># 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',)
</code></pre>

<p>And in Views for post you can do something like this</p>

<pre><code># 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})
</code></pre>

<p>In template you are doing the right thing.</p>

python django django-forms