2009-10-27 63 views
2

我正在嘗試使用不應修改的文件來創建模型。但該文件的評論可以。如何檢查在Django Admin中是否修改了FileField?

這是我做的,但我們不能修改評論。 如何測試新文件(使用瀏覽按鈕)是否已發送,並且僅在此情況下創建模型的新實例?如果沒有上傳新文件,請更新評論。

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

回答

7
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

非常感謝這正是:) – Natim 2009-10-28 03:05:41

相關問題