django-在哪里可以清理表单集的数据?

时间:2018-07-21 01:05:53

标签: django django-forms django-views formset django-2.0

我有一个视图功能,其中有两种形式,一种用于创建帖子,另一种是用于保存图像的表单集。在哪里可以清理表单集的数据?我想将图像调整为特定大小。我可以为表单集创建类似于类的类吗?

编辑: 表单集正在保存,但是图像没有被清理。

@login_required(login_url='/accounts/login/')
def postCreateView(request):
  ImageFormset = modelformset_factory(Image, fields=('image',), extra=3)
  if request.method == 'POST':
    form = PostCreateForm(request.POST)
    formset = ImageFormset(request.POST or None, request.FILES or None)
    if form.is_valid() and formset.is_valid():
      post = form.save(commit=False)
      post.user = request.user
      post.save()

      i = 1
      for instance in formset:
        try:
         photo = Image(post=post, 
                       image=instance.cleaned_data['image'], 
                       title=str(i))
          photo.save()
        except Exception as e:
          break
        return redirect('posts:post_list')
    else:
      form = PostCreateForm()
      formset = ImageFormset(queryset=Image.objects.none())
      context = {
              'form': form,
              'formset': formset,
      }
      return render(request, 'posts/post_form.html', context)

forms.py

class ImageForm(forms.ModelForm):
image = forms.ImageField(required=False)

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

    def clean_image(self, *args, **kwargs):
        from PIL import Image
        from io import BytesIO
        from django.core.files.uploadedfile import InMemoryUploadedFile
        from sys import getsizeof

        image = form.cleaned_data("image")
        if not image:
            image = None
            return image

        size = (140,140)

        im = Image.open(image)
        output = BytesIO()
        im.thumbnail(size)
        try:
            im.save(output, format='JPEG', quality=100)
            output.seek(0)
            image = InMemoryUploadedFile(output,'ImageField', "%s.jpg" %image.name.split('.')[0], 'image/jpeg', getsizeof(output), None)
        except:
            im.save(output, format='PNG', quality=100)
            output.seek(0)
            image = InMemoryUploadedFile(output,'ImageField', "%s.png" %image.name.split('.')[0], 'image/png', getsizeof(output), None)

        return image

1 个答案:

答案 0 :(得分:1)

通常,您将在与表单集相关联的forms.py中进行操作。您可以覆盖clean方法,并做任何您想做的事情。