Python django.db.models.FileField() Examples
The following are 30
code examples of django.db.models.FileField().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.db.models
, or try the search function
.
Example #1
Source File: utils.py From GTDWeb with GNU General Public License v2.0 | 7 votes |
def display_for_field(value, field): from django.contrib.admin.templatetags.admin_list import _boolean_icon from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE if field.flatchoices: return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE) # NullBooleanField needs special-case null-handling, so it comes # before the general null test. elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField): return _boolean_icon(value) elif value is None: return EMPTY_CHANGELIST_VALUE elif isinstance(field, models.DateTimeField): return formats.localize(timezone.template_localtime(value)) elif isinstance(field, (models.DateField, models.TimeField)): return formats.localize(value) elif isinstance(field, models.DecimalField): return formats.number_format(value, field.decimal_places) elif isinstance(field, models.FloatField): return formats.number_format(value) elif isinstance(field, models.FileField) and value: return format_html('<a href="{}">{}</a>', value.url, value) else: return smart_text(value)
Example #2
Source File: utils.py From openhgsenti with Apache License 2.0 | 6 votes |
def display_for_field(value, field, empty_value_display): from django.contrib.admin.templatetags.admin_list import _boolean_icon if field.flatchoices: return dict(field.flatchoices).get(value, empty_value_display) # NullBooleanField needs special-case null-handling, so it comes # before the general null test. elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField): return _boolean_icon(value) elif value is None: return empty_value_display elif isinstance(field, models.DateTimeField): return formats.localize(timezone.template_localtime(value)) elif isinstance(field, (models.DateField, models.TimeField)): return formats.localize(value) elif isinstance(field, models.DecimalField): return formats.number_format(value, field.decimal_places) elif isinstance(field, (models.IntegerField, models.FloatField)): return formats.number_format(value) elif isinstance(field, models.FileField) and value: return format_html('<a href="{}">{}</a>', value.url, value) else: return smart_text(value)
Example #3
Source File: utils.py From Hands-On-Application-Development-with-PyCharm with MIT License | 6 votes |
def display_for_field(value, field, empty_value_display): from django.contrib.admin.templatetags.admin_list import _boolean_icon if getattr(field, 'flatchoices', None): return dict(field.flatchoices).get(value, empty_value_display) # BooleanField needs special-case null-handling, so it comes before the # general null test. elif isinstance(field, models.BooleanField): return _boolean_icon(value) elif value is None: return empty_value_display elif isinstance(field, models.DateTimeField): return formats.localize(timezone.template_localtime(value)) elif isinstance(field, (models.DateField, models.TimeField)): return formats.localize(value) elif isinstance(field, models.DecimalField): return formats.number_format(value, field.decimal_places) elif isinstance(field, (models.IntegerField, models.FloatField)): return formats.number_format(value) elif isinstance(field, models.FileField) and value: return format_html('<a href="{}">{}</a>', value.url, value) else: return display_for_value(value, empty_value_display)
Example #4
Source File: utils.py From python with Apache License 2.0 | 6 votes |
def display_for_field(value, field, empty_value_display): from django.contrib.admin.templatetags.admin_list import _boolean_icon if getattr(field, 'flatchoices', None): return dict(field.flatchoices).get(value, empty_value_display) # NullBooleanField needs special-case null-handling, so it comes # before the general null test. elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField): return _boolean_icon(value) elif value is None: return empty_value_display elif isinstance(field, models.DateTimeField): return formats.localize(timezone.template_localtime(value)) elif isinstance(field, (models.DateField, models.TimeField)): return formats.localize(value) elif isinstance(field, models.DecimalField): return formats.number_format(value, field.decimal_places) elif isinstance(field, (models.IntegerField, models.FloatField)): return formats.number_format(value) elif isinstance(field, models.FileField) and value: return format_html('<a href="{}">{}</a>', value.url, value) else: return display_for_value(value, empty_value_display)
Example #5
Source File: utils.py From bioforum with MIT License | 6 votes |
def display_for_field(value, field, empty_value_display): from django.contrib.admin.templatetags.admin_list import _boolean_icon if getattr(field, 'flatchoices', None): return dict(field.flatchoices).get(value, empty_value_display) # NullBooleanField needs special-case null-handling, so it comes # before the general null test. elif isinstance(field, (models.BooleanField, models.NullBooleanField)): return _boolean_icon(value) elif value is None: return empty_value_display elif isinstance(field, models.DateTimeField): return formats.localize(timezone.template_localtime(value)) elif isinstance(field, (models.DateField, models.TimeField)): return formats.localize(value) elif isinstance(field, models.DecimalField): return formats.number_format(value, field.decimal_places) elif isinstance(field, (models.IntegerField, models.FloatField)): return formats.number_format(value) elif isinstance(field, models.FileField) and value: return format_html('<a href="{}">{}</a>', value.url, value) else: return display_for_value(value, empty_value_display)
Example #6
Source File: util.py From devops with MIT License | 6 votes |
def display_for_field(value, field): from xadmin.views.list import EMPTY_CHANGELIST_VALUE if field.flatchoices: return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE) # NullBooleanField needs special-case null-handling, so it comes # before the general null test. elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField): return boolean_icon(value) elif value is None: return EMPTY_CHANGELIST_VALUE elif isinstance(field, models.DateTimeField): return formats.localize(tz_localtime(value)) elif isinstance(field, (models.DateField, models.TimeField)): return formats.localize(value) elif isinstance(field, models.DecimalField): return formats.number_format(value, field.decimal_places) elif isinstance(field, models.FloatField): return formats.number_format(value) elif isinstance(field, models.FileField) and value: return format_html('<a href="{}">{}</a>', value.url, value) else: return smart_text(value)
Example #7
Source File: utils.py From python2017 with MIT License | 6 votes |
def display_for_field(value, field, empty_value_display): from django.contrib.admin.templatetags.admin_list import _boolean_icon if getattr(field, 'flatchoices', None): return dict(field.flatchoices).get(value, empty_value_display) # NullBooleanField needs special-case null-handling, so it comes # before the general null test. elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField): return _boolean_icon(value) elif value is None: return empty_value_display elif isinstance(field, models.DateTimeField): return formats.localize(timezone.template_localtime(value)) elif isinstance(field, (models.DateField, models.TimeField)): return formats.localize(value) elif isinstance(field, models.DecimalField): return formats.number_format(value, field.decimal_places) elif isinstance(field, (models.IntegerField, models.FloatField)): return formats.number_format(value) elif isinstance(field, models.FileField) and value: return format_html('<a href="{}">{}</a>', value.url, value) else: return display_for_value(value, empty_value_display)
Example #8
Source File: rearrange_files.py From coursys with GNU General Public License v3.0 | 6 votes |
def handle(self, *args, **options): app_name = options['app'] model_name = options['model'] field_name = options['field'] model = apps.get_model(app_name, model_name) fields = model._meta.get_fields() matching_fields = [f for f in fields if isinstance(f, models.FileField) and f.name == field_name] field = matching_fields[0] storage = field.storage for o in model.objects.all(): # threads aren't usually interrupted: https://stackoverflow.com/a/842567/6871666 t = Thread(target=move_file, args=(model, storage, field_name, field, o)) t.start() t.join()
Example #9
Source File: utils.py From django-unused-media with MIT License | 6 votes |
def get_file_fields(): """ Get all fields which are inherited from FileField """ # get models all_models = apps.get_models() # get fields fields = [] for model in all_models: for field in model._meta.get_fields(): if isinstance(field, models.FileField): fields.append(field) return fields
Example #10
Source File: test_cleanup.py From django-unused-media with MIT License | 5 votes |
def test_get_file_fields(self): file_fields = get_file_fields() expect(file_fields).to_be_instance_of(list).to_length(4) for f in file_fields: expect(f).to_be_instance_of(models.FileField) file_fields_names = [f.name for f in file_fields] expect(file_fields_names)\ .to_include('file_field')\ .to_include('image_field')\ .to_include('custom_field') expect(file_fields_names).Not.to_include('char_field') expect(file_fields_names).Not.to_include('active')
Example #11
Source File: wizard.py From ImitationTmall_Django with GNU General Public License v3.0 | 5 votes |
def _done(self): cleaned_data = self.get_all_cleaned_data() exclude = self.admin_view.exclude opts = self.admin_view.opts instance = self.admin_view.org_obj or self.admin_view.model() file_field_list = [] for f in opts.fields: if not f.editable or isinstance(f, models.AutoField) \ or not f.name in cleaned_data: continue if exclude and f.name in exclude: continue # Defer saving file-type fields until after the other fields, so a # callable upload_to can use the values from other fields. if isinstance(f, models.FileField): file_field_list.append(f) else: f.save_form_data(instance, cleaned_data[f.name]) for f in file_field_list: f.save_form_data(instance, cleaned_data[f.name]) instance.save() for f in opts.many_to_many: if f.name in cleaned_data: f.save_form_data(instance, cleaned_data[f.name]) self.admin_view.new_obj = instance
Example #12
Source File: test_ordinary_fields.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_upload_to_starts_with_slash(self): class Model(models.Model): field = models.FileField(upload_to='/somewhere') field = Model._meta.get_field('field') self.assertEqual(field.check(), [ Error( "FileField's 'upload_to' argument must be a relative path, not " "an absolute path.", obj=field, id='fields.E202', hint='Remove the leading slash.', ) ])
Example #13
Source File: test_generate_filename.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_filefield_generate_filename_with_upload_to(self): def upload_to(instance, filename): return 'some/folder/' + filename f = FileField(upload_to=upload_to) self.assertEqual( f.generate_filename(None, 'test with space.txt'), os.path.normpath('some/folder/test_with_space.txt') )
Example #14
Source File: test_ordinary_fields.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_primary_key(self): class Model(models.Model): field = models.FileField(primary_key=False, upload_to='somewhere') field = Model._meta.get_field('field') self.assertEqual(field.check(), [ Error( "'primary_key' is not a valid argument for a FileField.", obj=field, id='fields.E201', ) ])
Example #15
Source File: test_ordinary_fields.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_valid_case(self): class Model(models.Model): field = models.FileField(upload_to='somewhere') field = Model._meta.get_field('field') self.assertEqual(field.check(), [])
Example #16
Source File: test_ordinary_fields.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_valid_default_case(self): class Model(models.Model): field = models.FileField() self.assertEqual(Model._meta.get_field('field').check(), [])
Example #17
Source File: test_ordinary_fields.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_upload_to_callable_not_checked(self): def callable(instance, filename): return '/' + filename class Model(models.Model): field = models.FileField(upload_to=callable) field = Model._meta.get_field('field') self.assertEqual(field.check(), [])
Example #18
Source File: wizard.py From Dailyfresh-B2C with Apache License 2.0 | 5 votes |
def _done(self): cleaned_data = self.get_all_cleaned_data() exclude = self.admin_view.exclude opts = self.admin_view.opts instance = self.admin_view.org_obj or self.admin_view.model() file_field_list = [] for f in opts.fields: if not f.editable or isinstance(f, models.AutoField) \ or not f.name in cleaned_data: continue if exclude and f.name in exclude: continue # Defer saving file-type fields until after the other fields, so a # callable upload_to can use the values from other fields. if isinstance(f, models.FileField): file_field_list.append(f) else: f.save_form_data(instance, cleaned_data[f.name]) for f in file_field_list: f.save_form_data(instance, cleaned_data[f.name]) instance.save() for f in opts.many_to_many: if f.name in cleaned_data: f.save_form_data(instance, cleaned_data[f.name]) self.admin_view.new_obj = instance
Example #19
Source File: models.py From django-oss-storage with MIT License | 5 votes |
def file_delete(sender, instance, **kwargs): # Pass false so FileField doesn't save the model. print sender.myfile print instance instance.myfile.delete(False)
Example #20
Source File: test_ordinary_fields.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_valid_case(self): class Model(models.Model): field = models.FileField(upload_to='somewhere') field = Model._meta.get_field('field') self.assertEqual(field.check(), [])
Example #21
Source File: models.py From django-oss-storage with MIT License | 5 votes |
def photo_delete(sender, instance, **kwargs): # Pass false so FileField doesn't save the model. print sender.image print instance instance.image.delete(False)
Example #22
Source File: resources.py From cartoview with BSD 2-Clause "Simplified" License | 5 votes |
def obj_create(self, bundle, **kwargs): bundle = super(FileUploadResource, self).obj_create(bundle, **kwargs) for f in bundle.obj._meta.fields: if isinstance(f, models.FileField): file = bundle.request.FILES.get(f.name, None) if file: f.save_form_data(bundle.obj, file) return bundle
Example #23
Source File: wizard.py From online with GNU Affero General Public License v3.0 | 5 votes |
def _done(self): cleaned_data = self.get_all_cleaned_data() exclude = self.admin_view.exclude opts = self.admin_view.opts instance = self.admin_view.org_obj or self.admin_view.model() file_field_list = [] for f in opts.fields: if not f.editable or isinstance(f, models.AutoField) \ or not f.name in cleaned_data: continue if exclude and f.name in exclude: continue # Defer saving file-type fields until after the other fields, so a # callable upload_to can use the values from other fields. if isinstance(f, models.FileField): file_field_list.append(f) else: f.save_form_data(instance, cleaned_data[f.name]) for f in file_field_list: f.save_form_data(instance, cleaned_data[f.name]) instance.save() for f in opts.many_to_many: if f.name in cleaned_data: f.save_form_data(instance, cleaned_data[f.name]) self.admin_view.new_obj = instance
Example #24
Source File: test_writer.py From django-sqlserver with MIT License | 5 votes |
def test_serialize_local_function_reference_message(self): """ Make sure user is seeing which module/function is the issue """ class TestModel2(object): def upload_to(self): return "somewhere dynamic" thing = models.FileField(upload_to=upload_to) with self.assertRaisesMessage(ValueError, 'Could not find function upload_to in migrations.test_writer'): self.serialize_round_trip(TestModel2.thing)
Example #25
Source File: test_writer.py From django-sqlserver with MIT License | 5 votes |
def test_serialize_local_function_reference(self): """ Neither py2 or py3 can serialize a reference in a local scope. """ class TestModel2(object): def upload_to(self): return "somewhere dynamic" thing = models.FileField(upload_to=upload_to) with self.assertRaises(ValueError): self.serialize_round_trip(TestModel2.thing)
Example #26
Source File: tests.py From django-sqlserver with MIT License | 5 votes |
def test_file_field(self): field = models.FileField(upload_to="foo/bar") name, path, args, kwargs = field.deconstruct() self.assertEqual(path, "django.db.models.FileField") self.assertEqual(args, []) self.assertEqual(kwargs, {"upload_to": "foo/bar"}) # Test max_length field = models.FileField(upload_to="foo/bar", max_length=200) name, path, args, kwargs = field.deconstruct() self.assertEqual(path, "django.db.models.FileField") self.assertEqual(args, []) self.assertEqual(kwargs, {"upload_to": "foo/bar", "max_length": 200})
Example #27
Source File: deleteorphanedmedia.py From django-cloudinary-storage with MIT License | 5 votes |
def model_file_fields(self, model): """ Generator yielding all instances of FileField and its subclasses of a model. """ for field in model._meta.fields: if isinstance(field, models.FileField): yield field
Example #28
Source File: wizard.py From devops with MIT License | 5 votes |
def _done(self): cleaned_data = self.get_all_cleaned_data() exclude = self.admin_view.exclude opts = self.admin_view.opts instance = self.admin_view.org_obj or self.admin_view.model() file_field_list = [] for f in opts.fields: if not f.editable or isinstance(f, models.AutoField) \ or not f.name in cleaned_data: continue if exclude and f.name in exclude: continue # Defer saving file-type fields until after the other fields, so a # callable upload_to can use the values from other fields. if isinstance(f, models.FileField): file_field_list.append(f) else: f.save_form_data(instance, cleaned_data[f.name]) for f in file_field_list: f.save_form_data(instance, cleaned_data[f.name]) instance.save() for f in opts.many_to_many: if f.name in cleaned_data: f.save_form_data(instance, cleaned_data[f.name]) self.admin_view.new_obj = instance
Example #29
Source File: wizard.py From imoocc with GNU General Public License v2.0 | 5 votes |
def _done(self): cleaned_data = self.get_all_cleaned_data() exclude = self.admin_view.exclude opts = self.admin_view.opts instance = self.admin_view.org_obj or self.admin_view.model() file_field_list = [] for f in opts.fields: if not f.editable or isinstance(f, models.AutoField) \ or not f.name in cleaned_data: continue if exclude and f.name in exclude: continue # Defer saving file-type fields until after the other fields, so a # callable upload_to can use the values from other fields. if isinstance(f, models.FileField): file_field_list.append(f) else: f.save_form_data(instance, cleaned_data[f.name]) for f in file_field_list: f.save_form_data(instance, cleaned_data[f.name]) instance.save() for f in opts.many_to_many: if f.name in cleaned_data: f.save_form_data(instance, cleaned_data[f.name]) self.admin_view.new_obj = instance
Example #30
Source File: serializers.py From donation-tracker with Apache License 2.0 | 5 votes |
def handle_field(self, obj, field): if isinstance(field, models.FileField): value = field.value_from_object(obj) self._current[field.name] = value.url if value else '' elif isinstance(field, models.DecimalField): value = field.value_from_object(obj) self._current[field.name] = float(value) if value else value else: super(TrackerSerializer, self).handle_field(obj, field)