2

수정해서는 안되는 파일로 모델을 만들려고합니다. 그러나 파일의 주석이 될 수 있습니다.Django의 관리자에서 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

관련 문제