上传后自动将图像列表保存在ImageField中-Django 3 [英] Automatically save list of images in ImageField after upload - django 3

查看:36
本文介绍了上传后自动将图像列表保存在ImageField中-Django 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Django 3,我有以下模型:

Working with Django 3, I have the following model:

class Case(models.Model):
    slug = ShortUUIDField(max_length=3)
    title = models.CharField(max_length=50)
    #....
    ct = models.FileField(upload_to='files/')

class ModelImages(models.Model):
    case = models.ForeignKey(Case, on_delete=models.CASCADE,
                              related_name="slices_images")
    img = models.ImageField(upload_to='images/', editable=False)
    #....
    position = models.PositiveSmallIntegerField(editable=False)

一旦上传了"ct"(3D图像)(效果很好),我想提取"ct"的切片并将其另存为"ModelImages"类中的图像.

Once 'ct' (a 3D image) is uploaded (and this works perfectly), I want to extract the slices of the 'ct' and save them as images in 'ModelImages' class.

我使用名为"get_slices(ct)"的函数提取图像

I extract the images with a function called 'get_slices(ct)'

每个"ct"的图像都是数百张

The images are hundreds for each 'ct'

这应该是全自动的.

我该怎么办?

推荐答案

这是我对您的建议.使用后保存信号,可以在保存后自动处理" Case对象的3D图像.

Here is my suggestion for you. Use a post save signal that will 'automatically' process the 3D image of a Case object after it is saved.

from django.db.models.signals import post_save

class Case(models.Model):
    slug = ShortUUIDField(max_length=3)
    title = models.CharField(max_length=50)
    #....
    ct = models.FileField(upload_to='files/')

class ModelImages(models.Model):
    case = models.ForeignKey(Case, on_delete=models.CASCADE,
                              related_name="slices_images")
    img = models.ImageField(upload_to='images/', editable=False)
    #....
    position = models.PositiveSmallIntegerField(editable=False)

# This will be run anytime AFTER a Case object is saved.
def process_3d_image(sender, instance, created, **kwargs):
    # this is run only on first save (creation)
    if created:
        # if get_slices is creating and saving ModelImages objects you can pass
        # the instance (which is the Case object) for the foreign key as well.
        get_slices(instance.ct)

post_save.connect(process_3d_image, sender=Case)

当心!这将是一个同步任务,这意味着django将在返回响应之前等待该处理完成.看到有数百张图像,这可能需要一段时间..

Beware! This will be a synchronous task meaning django will wait for this processing to be done before returning a response. Seeing as there are hundreds of images this could take a while..







我的第二个建议是通过使用异步任务管理器(例如

My second suggestion would be to avoid this by using an asynchronous task manager such as Celery.

如果您设法设置Celery,则可以创建一个异步任务来保存 ct 中的图像,该任务将在后台完成,而不会阻塞您的Django应用.

If you manage to setup Celery you could create an asynchronous task for saving the images from ct that would be done in the background without blocking your django app.

tasks.py

from <module> import get_slices

@app.task()
def process_3d_image_async(case_ct):
    get_slices(case_ct)

修改的 post_save 函数(如果使用Celery)

modified post_save function if using Celery

from .tasks import process_3d_image_async

# This will be run whenever a Case object is saved.
def process_3d_image(sender, instance, created, **kwargs):
    # this is run only on first save (creation)
    if created:
        process_3d_image_async.delay(case_ct=instance.ct)

最后要注意的是,如果您异步执行此操作,您将想知道 Case 对象的文件是否已完成处理,所以我的最后建议是向您的Case添加BooleanField列默认值为False的模型,然后在切片后在异步任务中将其设置为true.

And last thing to note is if you do it asynchronously you'll want to know whether a Case object's file has finished being processed so my last suggestion would be to add a BooleanField column to your Case model with default value False that you then set to true in your async task after slicing.

但是,使用Celery只是一个建议!

However using Celery is only a suggestion!

这篇关于上传后自动将图像列表保存在ImageField中-Django 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆