关于Django-无法使用动态upload_to值为ImageField创建迁移和django无法生成迁移文件的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于'django.db.mode
关于Django-无法使用动态upload_to值为ImageField创建迁移和django无法生成迁移文件的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于'django.db.models' 没有属性 'StdImageField'、Django - ImageField 默认在反序列化时不起作用、Django Admin从Imagefield显示图像、Django ImageField upload_to路径等相关知识的信息别忘了在本站进行查找喔。
本文目录一览:- Django-无法使用动态upload_to值为ImageField创建迁移(django无法生成迁移文件)
- 'django.db.models' 没有属性 'StdImageField'
- Django - ImageField 默认在反序列化时不起作用
- Django Admin从Imagefield显示图像
- Django ImageField upload_to路径
Django-无法使用动态upload_to值为ImageField创建迁移(django无法生成迁移文件)
我刚刚将应用程序升级到1.7(实际上仍在尝试)。
这是我在models.py中拥有的:
def path_and_rename(path): def wrapper(instance, filename): ext = filename.split(''.'')[-1] # set filename as random string filename = ''{}.{}''.format(uuid4().hex, ext) # return the whole path to the file return os.path.join(path, filename) return wrapperclass UserProfile(AbstractUser): #... avatar = models.ImageField(upload_to=path_and_rename("avatars/"), null=True, blank=True, default="avatars/none/default.png", height_field="image_height", width_field="image_width")
当我尝试时makemigrations
,它抛出:
ValueError: Could not find function wrapper in webapp.models.Please note that due to Python 2 limitations, you cannot serialize unbound method functions (e.g. a method declaredand used in the same class body). Please move the function into the main module body to use migrations.
答案1
小编典典我不确定是否可以回答自己的问题,但我只是想出了(我认为)。
根据此错误报告,我编辑了代码:
from django.utils.deconstruct import deconstructible@deconstructibleclass PathAndRename(object): def __init__(self, sub_path): self.path = sub_path def __call__(self, instance, filename): ext = filename.split(''.'')[-1] # set filename as random string filename = ''{}.{}''.format(uuid4().hex, ext) # return the whole path to the file return os.path.join(self.path, filename)path_and_rename = PathAndRename("/avatars")
然后,在字段定义中:
avatar = models.ImageField(upload_to=path_and_rename, null=True, blank=True, default="avatars/none/default.png", height_field="image_height", width_field="image_width")
这对我有用。
'django.db.models' 没有属性 'StdImageField'
StdImageField
不是来自 django,而是来自第三方库,即 stdimage
。而且,您已经像这样导入了它:
from stdimage.models import StdImageField
因此,只需删除模型:
class CustomUser(AbstractUser):
picture = StdImageField(null=True,blank=True,upload_to="images",validators=[validate_file_size],size=(256,256))
,
StdImageField
属于 stdimage.models
模块,因此您将它与 picture = StdImageFIeld(…)
一起使用,而不是与 models.
前缀一起使用:
from stdimage.models import StdImageField
class CustomUser(AbstractUser):
picture = StdImageField(
null=True,upload_to=''images'',256)
)
,
StdImageField
不是 Django 的 models
API 的一部分,因此调用 picture = models.StdImageField
将不起作用。您已经导入了 StdImageField
,因此只需将模型字段更改为
picture = StdImageField(null=True,256))
Django - ImageField 默认在反序列化时不起作用
如何解决Django - ImageField 默认在反序列化时不起作用?
我使用包含单个图像和图像列表的 FormData 向 Axios 发送 POST 请求,如下所示:
React State:
"thumbnail": thumbnailImage,"gallery": [
galleryImage0,galleryImage1,galleryImage2
]
FormData:
formData.set("thumbnail",this.state.thumbnail);
for (let image of document.querySelector("#gallery").files){
formData.append("gallery",image);
}
注意:这部分可能没问题,因为即使使用 Postman 来制作请求,问题仍然存在
在 Django 中,如果发送了实际图像,我可以使用 DRF 完美地反序列化这些数据,但是如果例如“缩略图”不包含文件(用户没有选择文件),我会得到以下信息:>
[ErrorDetail(string=''The submitted data was not a file. Check the encoding type on the form.'',code=''invalid'')]
我真正想要的是使用模型中提供的默认值,如下所示:
class Item_ThumbnailImage(models.Model):
image = models.ImageField(upload_to=hash_image_thumbnail,default=''items/no-image.gif'',blank=True)
belongs_to = models.ForeignKey(
''Item'',related_name=''thumbnail'',on_delete=models.CASCADE)
我以这种方式准备好数据:
thumbnail_data = {
''belongs_to'': item.id,''image'': request.data.get("thumbnail")
}
这是反序列化:
thumbnail = Item_ThumbnailImageSerializer(data=thumbnail_data)
if thumbnail.is_valid():
thumbnail.save()
else:
print(thumbnail.errors)
序列化器:
class Item_ThumbnailImageSerializer(serializers.ModelSerializer):
class Meta:
model = Item_ThumbnailImage
fields = ''__all__''
我在序列化程序中试过这个:
image = serializers.ImageField(required=False,allow_empty_file=True,allow_null=True)
...伴随着这个:
thumbnail_data = {
''belongs_to'': item.id,''image'': request.data.get("thumbnail") if request.data.get("thumbnail") != '''' else None
}
...这显然使图像验证并成功保存,除了它没有图像。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
Django Admin从Imagefield显示图像
如何解决Django Admin从Imagefield显示图像?
当然。在模型类中,添加类似以下方法:
def image_tag(self):
from django.utils.html import escape
return u''<img src="%s" />'' % escape(<URL to the image>)
image_tag.short_description = ''Image''
image_tag.allow_tags = True
并在你的admin.py
添加:
fields = ( ''image_tag'', )
readonly_fields = (''image_tag'',)
给你ModelAdmin
。如果要限制编辑图像字段的能力,请确保将其添加到exclude属性。
注意:仅在readonly_fields中使用Django 1.8和’image_tag’时,它不会显示。仅在字段中使用“ image_tag”,它给出了未知字段的错误。为了正确显示,在字段和readonly_fields中都需要它。
解决方法
虽然我可以在list_display中显示上载的图像,但是是否可以在每个模型页面上执行此操作(就像在更改模型时获得的页面一样)?
快速样本模型为:
Class Model1(models.Model):
image = models.ImageField(upload_to=directory)
默认管理员显示上传图像的URL,但不显示图像本身。
Django ImageField upload_to路径
我在理解和使用Django的ImageField时遇到了麻烦。
我有一个模型:
class BlogContent(models.Model):
title = models.CharField(max_length=300)
image = models.ImageField(upload_to='static/static_dirs/images/')
description = models.TextField()
我的文件系统当前是:
src
|---main_project
|---app_that_contains_blog_content_model
|---static
|---static_dirs
|---images
当我运行服务器并转到“管理”页面时,可以添加BlogContent对象。为图像字段选择图像后,该图像具有临时名称。但是,保存该对象后,无法在upload_to路径指定的文件夹中找到该图像。
正确的方法是什么?
今天的关于Django-无法使用动态upload_to值为ImageField创建迁移和django无法生成迁移文件的分享已经结束,谢谢您的关注,如果想了解更多关于'django.db.models' 没有属性 'StdImageField'、Django - ImageField 默认在反序列化时不起作用、Django Admin从Imagefield显示图像、Django ImageField upload_to路径的相关知识,请在本站进行查询。
本文标签: