验证文件上传Django 1.11

时间:2019-07-05 10:52:29

标签: python django-1.11

试图验证上传文件的大小,但我收到AttributeError

试图在for循环中更改为file_size无效

Models.py

class Attachment(models.Model):
 app1=models.ForeignKey(
        'NewApps',
         null=False,
         verbose_name='Attachment',
         related_name='attachments',
         related_query_name='attachment'
                                        )
        doc=models.FileField(max_length=2000,verbose_name='Filename',upload_to='files/')

app3 \ views.py ..

   def forms_valid(self,form,inlines):

   for doc in form.doc:
      Attachment.objects.create(filename=doc,app1=self.object)
      self.object.attachment.create(doc=document)

app3 \ forms.py

   class app3ModelForm(forms.ModelForm):
     doc=forms.FileField(widget=forms.clearableFileInput(attr {'multiple'=True}),required=True)

  class Meta:
      model=app0
      fields=['doc']

  def __init__(self,*args,**kwargs):
     if 'doc' in self.data:
       for doc in self.instance.attachment.all():
          self.fields['doc'].queryset=Attachment.doc.objects.all()


 self.helper.layout=layout(
 DIV(
     'doc',value="upload"
     ),


 DIV(
     '{% if doc %}'
     '<p>uploaded file </p>'
     '<ul>'
     '{% for obj in document %}
     '<li>'
     '<a href="{{obj.url}}"></a>
     '</li>'
     '{% endfor %}'
     '{% endif %}'
     ),



   def clean(self):
     docs=self.cleaned_data.get('document')
       for doc in docs:
         if doc.file_size>30000
           self.add_error('doc','one of the files over the limit')

AttributeError:“ str”对象没有属性file_size

0 个答案:

没有答案