Django:从服务器上的现有文件在模型中手动创建图像字段

发布于 2024-10-04 17:50:16 字数 1273 浏览 5 评论 0原文

这简直要了我的命!

我正在使用 django-filebrowser,我想创建一个图库应用程序,利用它的上传功能来管理图像。

我有一个图库模型,允许用户在服务器上选择或创建一个目录,并将文件上传到该文件夹​​以显示在图库中。我想自动搜寻用户上传图像并选择的目录,然后自动为文件夹中的每个图像创建 Image 实例。

class Gallery(model.Models):
    gallerydirectory = FileBrowserField(...)
    title = ...
    description ...

class Image(model.Models):
    image_field = models.ImageField()

问题是 FileBrowser 表示图像的方式与 Django 不同,但我想使用 DJango ImageFields,因为我可以在模板端使用其他应用程序(sorl 缩略图)。

我拥有该文件所需的所有数据,即文件名、路径等,我只是无法让 Django 创建 ImageField 的实例,而无需再次实际上传图像。我只是想填充它。

我在这里看到了另一个线程,它建议以下内容:

for image in filebrowser_image_objects_list:
    f = File(open('path-to-file-on-server','r'))
    i = Image()
    i.image_field('filename.png',f.read())

但这是给我一个:

SuspiciousOperation error
Attempted access to '/filename.png' denied 

这表明路径没有被正确读取。我已经打印了文件对象的属性,并且它正在打开正确的图像,它只是没有传递到 ImageField

帮助将不胜感激!

更新

我已经放弃尝试完成这项工作,因为它太混乱了。我上面遇到的问题是我将 ImageField 的 upload_field 设置为“/”,但它没有被注意到,这意味着该文件正在写入“/something.png”。

我对其进行了修改,以便图像现在也使用 FileBrowserField 而不是 ImageField。

This is killing me!

I'm using django-filebrowser, and I want to create a gallery app that leverages it's upload capabilities to manage images.

I have a Gallery model that allows the user to select or create a directory on the server, and upload files to that folder to appear in the gallery. I want to automatically trawl the directory that the user has uploaded images to and selected, and then automatically create Image instances for each image in the folder.

class Gallery(model.Models):
    gallerydirectory = FileBrowserField(...)
    title = ...
    description ...

class Image(model.Models):
    image_field = models.ImageField()

The problem is that FileBrowser represents images differently to Django, but I want to use DJango ImageFields as I can then use other apps (sorl thumbnails) on the template end.

I have all the data necessary for the file i.e. filename, path etc, I just can't get Django to create an instance of an ImageField, without actually uploading the image again. I simply want to populate it.

I have seen another thread here which suggests the following:

for image in filebrowser_image_objects_list:
    f = File(open('path-to-file-on-server','r'))
    i = Image()
    i.image_field('filename.png',f.read())

but this is giving me a:

SuspiciousOperation error
Attempted access to '/filename.png' denied 

which suggests that the path isn't being read correctly. I've printed the attributes of the File Object, and it is opening the correct image, it just isn't getting passed on to the ImageField

Help would be greately appreciated!

UPDATE

I've given up trying to get this work as it's just too messy. The problem I had above was that I had set the upload_field of the ImageField to '/' and it had gone unnoticed meaning that the file was being written to '/something.png'.

I've modified it so that the Image is now using a FileBrowserField as well instead of an ImageField.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

_蜘蛛 2024-10-11 17:50:16

我可能会遗漏一些东西,但这对我有用:

from a1.models import Model1                                                               
from django.core.files.images import ImageFile
m = Model1.objects.create()                                                                
m.f1 = ImageFile(open("1.png", "rb"))      
m.save()

对于以下模型:

class Model1(models.Model):                 
    f1 = models.ImageField()

这样它不起作用:

m.f1('1.png', File.read(open('1.png', 'r')))

它说:

TypeError: 'ImageFieldFile' object is not callable

用 Django 1.7、1.11 检查。

I might be missing something, but this worked for me:

from a1.models import Model1                                                               
from django.core.files.images import ImageFile
m = Model1.objects.create()                                                                
m.f1 = ImageFile(open("1.png", "rb"))      
m.save()

for the following model:

class Model1(models.Model):                 
    f1 = models.ImageField()

This way it didn't work:

m.f1('1.png', File.read(open('1.png', 'r')))

It says:

TypeError: 'ImageFieldFile' object is not callable

Checked with Django 1.7, 1.11.

不甘平庸 2024-10-11 17:50:16

我将此标记为已回答,因为这是执行此操作的正确方法:

from django.core.files import File

image_model.image_field('path', File().read())

以编程方式保存图像到 Django ImageField

I'm marking this as answered, as this is the correct way to do this:

from django.core.files import File

image_model.image_field('path', File().read())

Programmatically saving image to Django ImageField

那请放手 2024-10-11 17:50:16

这对我来说适用于 Python3 和 Django 2

from urllib.request import urlopen
from urllib.parse import urlparse
from io import BytesIO
from django.core.files.images import ImageFile


from .models import ModelWithImageField

# Solving for SSL Error
import ssl
ssl._create_default_https_context = ssl._create_unverified_context

url = https://www.someurl.com/image.png?other_params_if_they_exist
image_file_name = urlparse(url).path.split('/')[-1]
image_file_content = BytesIO(urlopen(url).read())
ModelWithImageField().image_field.save(image_file_name, image_file_content)

This works for me on Python3 with Django 2

from urllib.request import urlopen
from urllib.parse import urlparse
from io import BytesIO
from django.core.files.images import ImageFile


from .models import ModelWithImageField

# Solving for SSL Error
import ssl
ssl._create_default_https_context = ssl._create_unverified_context

url = https://www.someurl.com/image.png?other_params_if_they_exist
image_file_name = urlparse(url).path.split('/')[-1]
image_file_content = BytesIO(urlopen(url).read())
ModelWithImageField().image_field.save(image_file_name, image_file_content)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文