【问题标题】:How to "link" already uploaded images to ImageField in Django?如何将已上传的图像“链接”到 Django 中的 ImageField?
【发布时间】:2020-05-24 18:54:16
【问题描述】:

我有这样的模型:

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 张图像,几乎每个符号都被命名为“imgslug”。我已经分批上传了。

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

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

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

class PlatformInline(admin.TabularInline):
    model = PlatformImages

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

现在我想将 ImageField 添加到 PlatformImages 模型中,但我需要:

  • 为每个独特的平台保存每个图像,以便在相应的符号 文件夹
  • 添加已上传的文件

而且我不知道如何实现这样的目标:

【问题讨论】:

    标签: python django django-admin


    【解决方案1】:

    在挖掘网络之后,我得出了以下解决方案:

    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) 要将已上传的图片链接到 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

    【讨论】:

      猜你喜欢
      • 2015-02-22
      • 1970-01-01
      • 2011-05-14
      • 2018-11-27
      • 1970-01-01
      • 2015-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多