如何知道在更新表单中更改了哪个字段-Django

时间:2018-06-29 05:39:35

标签: django python-3.x django-forms django-templates django-views

在上传图像之前,我需要压缩它们,为此,我在表单中使用PIL,但问题是我不知道哪个图像字段已更改(我有两个图像字段),请查看表单类

class UpdateProfileForm(forms.ModelForm):
    x = forms.FloatField(widget=forms.HiddenInput())
    y = forms.FloatField(widget=forms.HiddenInput())
    width = forms.FloatField(widget=forms.HiddenInput())
    height = forms.FloatField(widget=forms.HiddenInput())

    class Meta:
        model = UserProfileModel
        fields = ('profile_image','cover_image','about_self')
        widgets = {
            'file': forms.FileInput(attrs={
                'accept': 'image/*'  # this is not an actual validation! don't rely on that!
            })
        }

    def save(self):
        update_form = super(UpdateProfileForm, self).save()

        x = self.cleaned_data.get('x')
        y = self.cleaned_data.get('y')
        w = self.cleaned_data.get('width')
        h = self.cleaned_data.get('height')


        print(help(self.cleaned_data))

        image = Image.open(update_form.cover_image)
        cropped_image = image.crop((x, y, w+x, h+y))
        resized_image = cropped_image.resize((985, 370), Image.ANTIALIAS)
        resized_image.save(update_form.cover_image.path)

        return update_form

这一次我仅压缩cover_image,但是如果仅上传个人资料图片,则cover_image也将获得更改(当前cover_image中已应用更改),我需要的是如果表单类中的字段更改,则返回bool。

这是我的UserProfileModel

class UserProfileModel(models.Model):
    user                    = models.OneToOneField(
                                  'User',
                                  unique=True,
                                  on_delete=models.CASCADE,
                                  related_name='userprofilemodel',
                                  editable=False)
    fullname                = models.CharField(max_length=90)
    profile_image           = models.ImageField(
                                  upload_to=upload_profile_path, 
                                  default='media/images/defaults/profile_image.png')
    cover_image             = models.ImageField(
                                  upload_to=upload_cover_path, 
                                  default='media/images/defaults/cover_image.jpg')
    about_self              = models.TextField(
                                  max_length = 500,
                                  default='Hello, now I am affiliated with Bekaim.')
    first_name              = models.CharField(
                                  max_length=50,
                                  null=True,
                                  blank=True)
    last_name               = models.CharField(
                                  max_length=50,
                                  null=True,
                                  blank=True)
    user_location           = models.ForeignKey(
                                  Location,
                                  related_name='user_location',
                                  on_delete=models.CASCADE,
                                  null=True,blank=True)
    slug                    = models.SlugField(
                                  max_length=255,
                                  unique=True,
                                  null=True,
                                  blank=True)
    alternavite_mobile_number= PhoneNumberField(
                                  verbose_name="Alternative number",
                                  max_length=13,
                                  unique=True,
                                  null=True)
    is_phone_pin_send       = models.BooleanField(default=False)
    verified                = models.BooleanField(default=False)
    phone_pin               = RandomPinField(length=6)
    email_activation_key    = models.CharField(max_length=40)
    # email_key_expires       = models.DateTimeField()

    def fullname_saver(self):
        self.fullname = self.user.name

    def get_absolute_url(self):
        return reverse('accounts:profile',
                    kwargs={'username': self.user.display_username})

    def __str__(self):
        return self.user.display_username

0 个答案:

没有答案