开发者

How to check if a FileField has been modified in the Admin of Django?

I am trying to do a model with a file that shouldn't be modified. But the comment of the file can be.

Here is what I did, but we cannot modify the comment. How can I test if a new file (using the browse button) as been sent and in this case only, create a new instance of the model ? If no upload of a new file, update the comment.

admin.py

class CGUAdminForm(forms.ModelForm):
    class Meta:
        model = ConditionsUtilisation

    def clean_file(self):
        if self.instance and self.instance.pk is not None:
            raise forms.ValidationError(_(u'You cannot modify the file. Thank you to create a new instance.'))
        # do something that validates your data
        return self.cleaned_data["file"]

class CGUAdmin(admin.ModelAdmin):
    form = CGUAdminForm

admin.site.register(ConditionsUtilisation, CGUAdmin)

models.py

class ConditionsUtilisation(models.Model):
    date 开发者_Go百科= models.DateField(_(u'Date d\'upload'), editable=False, auto_now_add=True)
    comment = models.TextField(_(u'Commentaire de modification'))
    file = models.FileField(_(u'CGU'), upload_to='subscription/cgu/', storage=CGUFileSystemStorage())


if 'file' in form.changed_data:
     """
     File is changed
     """
     raise forms.ValidationError("No, don't change the file because blah blah")
else:
     """
     File is not changed
     """
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜