Django 如何"link" 上传图片到ImageField?

How to "link" already uploaded images to ImageField in Django?

我有这样的模型:

class Symbol(models.Model): 
    slug = models.SlugField(unique=True) #uniqe
    name = models.CharField(max_length=255, unique=True)
    imgslug = models.CharField(unique=False, max_length=255, blank=True) #uniqe

    def __str__(self):
        return self.name

class Platform(models.Model):
    name = models.CharField(max_length=50, unique=True)

    def __str__(self):
        return self.name

class PlatformImages(models.Model):
    symbol_id  = models.ForeignKey('Symbol', on_delete=models.CASCADE,) 
    platform_id = models.ForeignKey('Platform', on_delete=models.PROTECT,)

    class Meta:
        indexes = [
            models.Index(fields=['symbol_id', 'symbol_id']),
        ]
        constraints = [
            models.UniqueConstraint(fields=['symbol_id', 'platform_id'], name='unique_symbol_img_per_platform')
        ]

    def __str__(self): 
        return str(self.platform_id)

    def get_platform_name(self):
        return self.platform_id

我在媒体文件夹中有每个平台的文件夹,例如:media/apple/、media/google/、media/twitter/ 等

每个文件夹中有大约 1000-3000 张图像,几乎每个 Symbol 都以 "imgslug" 命名。我已经批量上传了。

我在模板中使用它来显示每个平台符号的所有可用图像:

{% for platform in platforms %}
    <img src="{{ MEDIA_PREFIX }}{{platform}}/{{symbol.imgslug}}" >
    <h2 class="">{{platform|title}}</h2>
{% endfor %}

问题是现在我需要通过管理工具 edit/upload 新图像。 我将此代码添加到 admin.py 以编辑图像到平台的映射:

class PlatformInline(admin.TabularInline):
    model = PlatformImages

class SymbolAdmin(admin.ModelAdmin):
    inlines = [
        PlatformInline,
    ]

现在我想添加 ImageFieldPlatformImages 模型,但我需要:

而且我不知道该怎么做才能实现这样的目标:

在网上搜索后,我得出以下解决方案:

  1. 在添加到 models.py:

    的相应文件夹中保存符号的每个唯一平台的每个图像
    class Platform(models.Model):
        name = models.CharField(max_length=50, unique=True)
        folder = models.SlugField(unique=True, null=True, blank=True)
    
    def platform_directory_path(instance, filename):
        return '{0}/{1}'.format(instance.platform_id.folder, filename)
    
    class PlatformImages(models.Model):
        symbol_id  = models.ForeignKey('Symbol', on_delete=models.CASCADE,)
        platform_id = models.ForeignKey('Platform', on_delete=models.PROTECT,)
        image = models.ImageField(upload_to=platform_directory_path, null=True, blank=True)
    

2) to link 已经上传图片到ImageField,打开shell

python3 manage.py shell

并执行脚本:

from django.db import models
images = PlatformImages.objects.all()
for image in images:
    platform = image.platform_id.name
    symbol = image.symbol_id.imgslug
    imagePath = str(platform +'/'+symbol)
    print (str(image.id) + " : " + imagePath)
    image.image = imagePath
    image.save()

是什么帮助我找到了解决方案:

Django docs - upload_to may also be a callable, such as a function

How to link already existing image to an ImageField