本文整理汇总了Python中django.db.models.FileField方法的典型用法代码示例。如果您正苦于以下问题:Python models.FileField方法的具体用法?Python models.FileField怎么用?Python models.FileField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models
的用法示例。
在下文中一共展示了models.FileField方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FileField [as 别名]
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()
示例2: display_for_field
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FileField [as 别名]
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)
示例3: display_for_field
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FileField [as 别名]
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)
示例4: display_for_field
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FileField [as 别名]
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)
示例5: display_for_field
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FileField [as 别名]
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)
示例6: display_for_field
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FileField [as 别名]
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)
示例7: display_for_field
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FileField [as 别名]
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)
示例8: get_file_fields
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FileField [as 别名]
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
示例9: handle_field
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FileField [as 别名]
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)
示例10: sendfile
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FileField [as 别名]
def sendfile(self, upfile, response):
"""
Send the contents of the file as the response, given a FileField object to read from.
"""
path, filename = os.path.split(upfile.name)
response['Content-Disposition'] = 'inline; filename="' + filename + '"'
try:
upfile.open('rb')
except IOError:
response['Content-type'] = "text/plain"
response.write("File missing. It has likely been archived.")
return
for data in upfile:
response.write(data)
示例11: add_arguments
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FileField [as 别名]
def add_arguments(self, parser):
parser.add_argument('app', type=str, help='the app to work in')
parser.add_argument('model', type=str, help='the model class to work on')
parser.add_argument('field', type=str, help='the FileField in the model')
示例12: _done
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FileField [as 别名]
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
示例13: display_for_field
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FileField [as 别名]
def display_for_field(value, field, html=True, only_date=True):
if getattr(field, 'flatchoices', None):
if html and field.name == 'color' and value:
return make_color_icon(value)
return dict(field.flatchoices).get(value, '')
elif html and (isinstance(field, (models.BooleanField, models.NullBooleanField))):
return make_boolean_icon(value)
elif isinstance(field, (models.BooleanField, models.NullBooleanField)):
boolchoice = {False: "否", True: "是"}
return boolchoice.get(value)
elif value is None:
return ""
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.ForeignKey) and value:
rel_obj = field.related_model.objects.get(pk=value)
if html and COLOR_FK_FIELD and isinstance(rel_obj, Option):
text_color = rel_obj.color
if not text_color:
text_color = 'text-info'
safe_value = format_html(
'<span class="text-{}">{}</span>', text_color, rel_obj.text)
return safe_value
return force_text(rel_obj)
elif isinstance(field, models.TextField) and value:
return force_text(value)
elif isinstance(field, models.DateTimeField):
if only_date:
return formats.date_format(value)
return formats.localize(timezone.template_localtime(value))
elif isinstance(field, (models.DateField, models.TimeField)):
return formats.localize(value)
elif isinstance(field, models.FileField) and value:
return format_html('<a href="{}">{}</a>', value.url, value)
else:
return display_for_value(value)
示例14: construct_instance
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FileField [as 别名]
def construct_instance(form, instance, fields=None, exclude=None):
"""
Constructs and returns a model instance from the bound ``form``'s
``cleaned_data``, but does not save the returned instance to the
database.
"""
from django.db import models
opts = instance._meta
cleaned_data = form.cleaned_data
file_field_list = []
for f in opts.fields:
if not f.editable or isinstance(f, models.AutoField) \
or f.name not in cleaned_data:
continue
if fields is not None and f.name not in fields:
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])
return instance
示例15: render_change_form
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FileField [as 别名]
def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None):
opts = self.model._meta
app_label = opts.app_label
preserved_filters = self.get_preserved_filters(request)
form_url = add_preserved_filters({'preserved_filters': preserved_filters, 'opts': opts}, form_url)
view_on_site_url = self.get_view_on_site_url(obj)
context.update({
'add': add,
'change': change,
'has_add_permission': self.has_add_permission(request),
'has_change_permission': self.has_change_permission(request, obj),
'has_delete_permission': self.has_delete_permission(request, obj),
'has_file_field': True, # FIXME - this should check if form or formsets have a FileField,
'has_absolute_url': view_on_site_url is not None,
'absolute_url': view_on_site_url,
'form_url': form_url,
'opts': opts,
'content_type_id': get_content_type_for_model(self.model).pk,
'save_as': self.save_as,
'save_on_top': self.save_on_top,
'to_field_var': TO_FIELD_VAR,
'is_popup_var': IS_POPUP_VAR,
'app_label': app_label,
})
if add and self.add_form_template is not None:
form_template = self.add_form_template
else:
form_template = self.change_form_template
request.current_app = self.admin_site.name
return TemplateResponse(request, form_template or [
"admin/%s/%s/change_form.html" % (app_label, opts.model_name),
"admin/%s/change_form.html" % app_label,
"admin/change_form.html"
], context)