具有多重上传的 django 形式的多个图像

Multiple images in django form with multiupload

我需要将多张 django 格式的图像添加到一个模型中。我做了一项研究,对于 django 之外的形式,我尝试设置 django-multiupload.

我的models.py:

class Profile(models.Model):
    ...
    ...
    first = models.ImageField("first", upload_to='first')
    second = models.ImageField("second", upload_to='second')
    ...

在forms.py中:

class AddForm(forms.ModelForm):
    first = MultiImageField(min_num=1, max_num=20)
    second = MultiImageField(min_num=1, max_num=4)

在views.py中:

class UploadView(FormView):
    template_name = 'finder/submit.html'
    form_class = AddForm
    success_url = '/'

    def form_valid(self, form):
        for each in form.cleaned_data['first']:
            Profile.objects.create(first=each)
        for each in form.cleaned_data['second']:
            Profile.objects.create(second=each)
        return super(UploadView, self).form_valid(form)

并且在提交表单时,此表单会创建多个仅填充 first/second 字段的配置文件对象。

如何只创建一个具有剩余字段(first/second 除外)和多个 first/second 字段的模型?

这是我在添加 multiupload 之前基于函数的视图,但我无法使其工作,也许以某种方式更改它更容易?

def add_form(request, *args, **kwargs):
    if request.method == "POST":
        form = AddForm(request.POST)
        if form.is_valid():
            profile = form.save(commit=False)
            profile.save()
            return redirect('/', pk=profile.pk)
    else:
        form = AddForm()
    return render(request, 'finder/submit.html', {'form': form})

我从未使用过 Django-multiupload,但我碰巧阅读了一些文档。

如果您想为您的用户模型保存多个文件,您可能需要创建另一个模型来容纳这些文件并向配置文件模型添加一个外键。 从 Profile 模型中删除第一个和第二个字段。它会导致您使用相同的数据创建多个配置文件以容纳多个图像。

简单的例子,

class Image(models.Model):
    image = models.FileField()
    profile = models.ForeignKey(Profile, related_name='images')
    is_first = models.BooleanField(default=False)
    is_second = models.BooleanField(default=False)

然后,编辑表单中的保存方法,

class AddForm(forms.ModelForm):
    first = MultiImageField(min_num=1, max_num=20)
    second = MultiImageField(min_num=1, max_num=4)

    class Meta:
        model = Profile
        fields = (.......... 'first', 'second')

    def save(self, commit=True):
        first_images = self.cleaned_data.pop('first')
        second_images = self.cleaned_data.pop('second')
        instance = super(AddForm, self).save()
        for each in first_images:
           first = Image(image=each, profile=instance, is_first=True)
           first.save()

        for each in second_images:
           second = Image(image=each, profile=instance, is_second=True)
           second.save()

        return instance

然后,在视图上,编辑视图,

class UploadView(FormView):
    template_name = 'finder/submit.html' 
    form_class = AddForm 
    success_url = '/' 

    def form_valid(self, form): 
        instance = form.save(commit=False)
        instance.user = self.request.user
        instance.save()

        return super(UploadView, self).form_valid(form)

或者在基于函数的视图中,

def add_form(request, *args, **kwargs): 
    if request.method == "POST": 
        form = AddForm(request.POST)
        if form.is_valid(): 
            profile = form.save(commit=False) 
            profile.user = request.user
            profile.save() 
            return redirect('/', pk=profile.pk) 
    else: 
        form = AddForm() 
    return render(request, 'finder/submit.html', {'form': form})