本文整理汇总了Python中django.db.models.ImageField方法的典型用法代码示例。如果您正苦于以下问题:Python models.ImageField方法的具体用法?Python models.ImageField怎么用?Python models.ImageField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models
的用法示例。
在下文中一共展示了models.ImageField方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ImageField [as 别名]
def handle(self, *args, **options):
storage = DefaultStorage()
for model in _get_models(['shapes', 'photos', 'shapes']):
has_images = False
# transfer image fields
for f in model._meta.fields:
if isinstance(f, models.ImageField):
has_images = True
if hasattr(storage, 'transfer'):
filenames = model.objects.all() \
.values_list(f.name, flat=True)
print '%s: %s' % (model, f)
for filename in progress.bar(filenames):
if filename and storage.local.exists(filename):
storage.transfer(filename)
# transfer thumbs
if has_images:
print '%s: thumbnails' % model
ids = model.objects.all().values_list('id', flat=True)
ct_id = ContentType.objects.get_for_model(model).id
for id in progress.bar(ids):
ensure_thumbs_exist_task.delay(ct_id, id)
示例2: to_json
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ImageField [as 别名]
def to_json(self):
"""
序列模型实例,让具体的模型实例可以 json 化
属性说明:
a = RecodeImage.objects.get(recode_number="5+8")
a._meta --> (<django.db.models.fields.AutoField: id>,
<django.db.models.fields.CharField: recode_number>,
<django.db.models.fields.files.ImageField: recode_image_path>,
<django.db.models.fields.DateTimeField: add_time>)
这是一个可迭代的对象,通过遍历,取得每个字段的名字(field.name)
getattr(self, attr) --> 获取模型实例的字段值
ImageField 字段使用 str() 字符串化
"""
fields = []
for field in self._meta.fields:
fields.append(field.name)
d = {}
for attr in fields:
d[attr] = str(getattr(self, attr))
return json.dumps(d)
示例3: test_pillow_installed
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ImageField [as 别名]
def test_pillow_installed(self):
try:
from PIL import Image # NOQA
except ImportError:
pillow_installed = False
else:
pillow_installed = True
class Model(models.Model):
field = models.ImageField(upload_to='somewhere')
field = Model._meta.get_field('field')
errors = field.check()
expected = [] if pillow_installed else [
Error(
'Cannot use ImageField because Pillow is not installed.',
hint=('Get Pillow at https://pypi.org/project/Pillow/ '
'or run command "pip install Pillow".'),
obj=field,
id='fields.E210',
),
]
self.assertEqual(errors, expected)
示例4: get_field_attrs
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ImageField [as 别名]
def get_field_attrs(self, attrs, db_field, **kwargs):
if isinstance(db_field, models.ImageField):
attrs['widget'] = AdminImageWidget
attrs['form_class'] = AdminImageField
self.include_image = True
return attrs
示例5: get_field_result
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ImageField [as 别名]
def get_field_result(self, result, field_name):
if isinstance(result.field, models.ImageField):
if result.value:
img = getattr(result.obj, field_name)
result.text = mark_safe('<a href="%s" target="_blank" title="%s" data-gallery="gallery"><img src="%s" class="field_img"/></a>' % (img.url, result.label, img.url))
self.include_image = True
return result
# Media
示例6: test_create_translation_field_has_correct_descriptor
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ImageField [as 别名]
def test_create_translation_field_has_correct_descriptor(self):
field = create_translation_field(models.fields.CharField(), "en")
assert field.descriptor_class == TranslationDescriptor
field = create_translation_field(models.FileField(), "en")
assert field.descriptor_class == files.FileTranslationDescriptor
field = create_translation_field(models.ImageField(), "en")
assert field.descriptor_class == files.FileTranslationDescriptor
field = create_translation_field(models.fields.TextField(), "en")
assert field.descriptor_class == TranslationDescriptor
示例7: formfield
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ImageField [as 别名]
def formfield(self, **kwargs):
defaults = {'form_class': forms.ImageField}
defaults.update(kwargs)
return super().formfield(**defaults)
示例8: test_should_image_convert_string
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ImageField [as 别名]
def test_should_image_convert_string():
assert_conversion(models.ImageField, graphene.String)
示例9: render
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ImageField [as 别名]
def render(self, obj):
src = helper.call_or_get(obj, self.attr)
if isinstance(src, ModelImageField):
src = settings.MEDIA_URL + src
p_params = {}
for key in self.params.keys():
p_params[key] = helper.call_or_get(obj, self.params[key])
p_params['src'] = src
return '<img%s/>' % (
flatatt(p_params)
)
示例10: test_image_field
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ImageField [as 别名]
def test_image_field(self):
field = models.ImageField(upload_to="foo/barness", width_field="width", height_field="height")
name, path, args, kwargs = field.deconstruct()
self.assertEqual(path, "django.db.models.ImageField")
self.assertEqual(args, [])
self.assertEqual(kwargs, {"upload_to": "foo/barness", "width_field": "width", "height_field": "height"})