【问题标题】:How to check if a FileField has been modified in the Admin of Django?如何在 Django 的管理员中检查 FileField 是否已被修改?
【发布时间】:2010-12-10 08:44:15
【问题描述】:

我正在尝试使用不应修改的文件创建模型。但是文件的注释可以。

这是我所做的,但我们无法修改评论。 如何测试是否发送了新文件(使用浏览按钮),并且仅在这种情况下,创建模型的新实例?如果没有上传新文件,请更新评论。

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 = 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())

【问题讨论】:

    标签: python django django-admin django-forms django-file-upload


    【解决方案1】:
    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
         """
    

    【讨论】:

      猜你喜欢
      • 2013-10-31
      • 2013-12-16
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 2015-05-14
      • 2012-02-09
      • 2020-01-08
      相关资源
      最近更新 更多