本文整理汇总了Python中django.forms.FileField方法的典型用法代码示例。如果您正苦于以下问题:Python forms.FileField方法的具体用法?Python forms.FileField怎么用?Python forms.FileField使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.forms
的用法示例。
在下文中一共展示了forms.FileField方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: field_value
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import FileField [as 别名]
def field_value(field):
"""
Returns the value for this BoundField, as rendered in widgets.
"""
if field.form.is_bound:
if isinstance(field.field, FileField) and field.data is None:
val = field.form.initial.get(field.name, field.field.initial)
else:
val = field.data
else:
val = field.form.initial.get(field.name, field.field.initial)
if isinstance(val, collections.Callable):
val = val()
if val is None:
val = ''
return val
示例2: clean
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import FileField [as 别名]
def clean(self, value, initial):
files = value['files']
cleared = value['cleared']
if not files and not cleared:
return initial
new = [
FileField(validators=[
FileExtensionValidator(allowed_extensions=settings.FILE_ALLOWED_EXTENSIONS)
]).clean(file, initial) for file in files
]
if initial:
old = [file for i, file in enumerate(initial) if i not in cleared]
else:
old = []
return old + new
示例3: process_form_submission
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import FileField [as 别名]
def process_form_submission(self, form):
cleaned_data = form.cleaned_data
for name, field in form.fields.items():
if isinstance(field, FileField):
file_data = cleaned_data[name]
if file_data:
file_name = file_data.name
file_name = webform_storage.generate_filename(file_name)
upload_to = os.path.join('webform', str(self.id), file_name)
saved_file_name = webform_storage.save(upload_to, file_data)
file_details_dict = {name: webform_storage.url(saved_file_name)}
cleaned_data.update(file_details_dict)
else:
del cleaned_data[name]
form_data = json.dumps(cleaned_data, cls=DjangoJSONEncoder)
return self.get_submission_class().objects.create(
form_data=form_data,
page=self,
)
示例4: test_full_clear
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import FileField [as 别名]
def test_full_clear(self):
"""
Integration happy-path test that a model FileField can actually be set
and cleared via a ModelForm.
"""
class DocumentForm(forms.ModelForm):
class Meta:
model = Document
fields = '__all__'
form = DocumentForm()
self.assertIn('name="myfile"', str(form))
self.assertNotIn('myfile-clear', str(form))
form = DocumentForm(files={'myfile': SimpleUploadedFile('something.txt', b'content')})
self.assertTrue(form.is_valid())
doc = form.save(commit=False)
self.assertEqual(doc.myfile.name, 'something.txt')
form = DocumentForm(instance=doc)
self.assertIn('myfile-clear', str(form))
form = DocumentForm(instance=doc, data={'myfile-clear': 'true'})
doc = form.save(commit=False)
self.assertFalse(doc.myfile)
示例5: test_filefield_changed
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import FileField [as 别名]
def test_filefield_changed(self):
"""
The value of data will more than likely come from request.FILES. The
value of initial data will likely be a filename stored in the database.
Since its value is of no use to a FileField it is ignored.
"""
f = FileField()
# No file was uploaded and no initial data.
self.assertFalse(f.has_changed('', None))
# A file was uploaded and no initial data.
self.assertTrue(f.has_changed('', {'filename': 'resume.txt', 'content': 'My resume'}))
# A file was not uploaded, but there is initial data
self.assertFalse(f.has_changed('resume.txt', None))
# A file was uploaded and there is initial data (file identity is not dealt
# with here)
self.assertTrue(f.has_changed('resume.txt', {'filename': 'resume.txt', 'content': 'My resume'}))
示例6: make_entry_field
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import FileField [as 别名]
def make_entry_field(self, fieldsubmission=None):
f = forms.FileField(required=self.config['required'],
label=self.config['label'],
help_text=self.config['help_text'],
widget=_ClearableFileInput())
f.filesub = None
if fieldsubmission:
file_sub = fieldsubmission.file_sub()
if file_sub:
f.filesub = file_sub
f.initial = file_sub.file_attachment
f.initial.file_sub = fieldsubmission.file_sub()
return f
示例7: __eq__
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import FileField [as 别名]
def __eq__(self, other):
# Older code may be expecting FileField values to be simple strings.
# By overriding the == operator, it can remain backwards compatibility.
if hasattr(other, 'name'):
return self.name == other.name
return self.name == other
示例8: __init__
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import FileField [as 别名]
def __init__(self, verbose_name=None, name=None, upload_to='', storage=None, **kwargs):
self._primary_key_set_explicitly = 'primary_key' in kwargs
self._unique_set_explicitly = 'unique' in kwargs
self.storage = storage or default_storage
self.upload_to = upload_to
if callable(upload_to):
self.generate_filename = upload_to
kwargs['max_length'] = kwargs.get('max_length', 100)
super(FileField, self).__init__(verbose_name, name, **kwargs)
示例9: check
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import FileField [as 别名]
def check(self, **kwargs):
errors = super(FileField, self).check(**kwargs)
errors.extend(self._check_unique())
errors.extend(self._check_primary_key())
return errors
示例10: deconstruct
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import FileField [as 别名]
def deconstruct(self):
name, path, args, kwargs = super(FileField, self).deconstruct()
if kwargs.get("max_length", None) == 100:
del kwargs["max_length"]
kwargs['upload_to'] = self.upload_to
if self.storage is not default_storage:
kwargs['storage'] = self.storage
return name, path, args, kwargs
示例11: get_internal_type
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import FileField [as 别名]
def get_internal_type(self):
return "FileField"
示例12: get_prep_value
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import FileField [as 别名]
def get_prep_value(self, value):
"Returns field's value prepared for saving into a database."
value = super(FileField, self).get_prep_value(value)
# Need to convert File objects provided via a form to unicode for database insertion
if value is None:
return None
return six.text_type(value)
示例13: pre_save
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import FileField [as 别名]
def pre_save(self, model_instance, add):
"Returns field's value just before saving."
file = super(FileField, self).pre_save(model_instance, add)
if file and not file._committed:
# Commit the file to storage prior to saving the model
file.save(file.name, file, save=False)
return file
示例14: contribute_to_class
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import FileField [as 别名]
def contribute_to_class(self, cls, name, **kwargs):
super(FileField, self).contribute_to_class(cls, name, **kwargs)
setattr(cls, self.name, self.descriptor_class(self))
示例15: formfield
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import FileField [as 别名]
def formfield(self, **kwargs):
defaults = {'form_class': forms.FileField, 'max_length': self.max_length}
# If a file has been provided previously, then the form doesn't require
# that a new file is provided this time.
# The code to mark the form field as not required is used by
# form_for_instance, but can probably be removed once form_for_instance
# is gone. ModelForm uses a different method to check for an existing file.
if 'initial' in kwargs:
defaults['required'] = False
defaults.update(kwargs)
return super(FileField, self).formfield(**defaults)