简体   繁体   English

Django:在其他应用程序中使用ImageFields吗?

[英]Django: Using ImageFields in other apps?

I have some apps where pictures are uploaded. 我有一些可以上传图片的应用。 Now that almost the same code is present multiple times in every app, I want the images to be loaded from an app named images. 既然每个应用程序中都多次出现几乎相同的代码,我希望从名为images的应用程序中加载图像。 is it possible to connect the other apps so that they use the ImageFields of the app1[images] ShareImage class? 是否可以连接其他应用程序,以便它们使用app1 [images] ShareImage类的ImageFields?

If not, how can I use the method image_resize and the two @receiver functions so that they can be used by multiple apps, and I only have one code: 如果没有,我如何使用方法image_resize和两个@receiver函数,以便它们可以被多个应用程序使用,而且我只有一个代码:

I've tried heredity before, but I can't get away with it. 我曾经尝试过遗传,但是我无法摆脱它。

i also tried to import the ImageFields into another class via Import. 我也尝试通过Import将ImageFields导入另一个类。

app1: APP1:

import os

from io import BytesIO
from PIL import Image
from django.db import models
from django.dispatch import receiver
from django.core.files.base import ContentFile
from .storage import OverwriteStorage


class ShareImage(models.Model):
    image = models.ImageField(
        upload_to='images/',
        verbose_name='Bild',
        storage=OverwriteStorage(),
    )

    thumbnail = models.ImageField(
        upload_to='images/',
        editable=False,
        storage=OverwriteStorage(),
    )

    def save(self, *args, **kwargs):
        if not os.path.exists(self.image.path) and not self.images_resize():
            raise Exception('Error: Thumbnail not created')

        super(ShareImage, self).save(*args, **kwargs)

    def images_resize(self):
        size = (288, 162)

        image = Image.open(self.image)
        image.thumbnail(size, Image.ANTIALIAS)

        name, extension = os.path.splitext(self.image.name)
        # to prevent django from creating new folders incorrectly
        name = os.path.basename(name)
        extension = extension.lower()
        file_name = name + '_thumbnail' + extension

        if extension in ['.jpg', '.jpeg']:
            file_extension = 'JPEG'
        elif extension == '.png':
            file_extension = 'PNG'
        elif extension == '.gif':
            file_extension = 'GIF'
        else:
            raise Exception('Error: Files extension not supported')

        temp = BytesIO()
        image.save(temp, file_extension)
        temp.seek(0)

        if not os.path.exists(self.image.path):
            self.thumbnail.save(
                file_name, ContentFile(temp.read()), save=False
            )
            return True


@receiver(models.signals.post_delete, sender=ShareImage)
def auto_delete_images_on_delete(instance, **kwargs):
    if instance.image:
        if os.path.isfile(instance.image.path):
            os.remove(instance.image.path)

    if instance.thumbnail:
        if os.path.isfile(instance.thumbnail.path):
            os.remove(instance.thumbnail.path)


@receiver(models.signals.pre_save, sender=ShareImage)
def auto_delete_images_on_change(instance, **kwargs):
    if not instance.pk:
        return False

    try:
        old_image = ShareImage.objects.get(pk=instance.pk).image
        old_thumb = ShareImage.objects.get(pk=instance.pk).thumbnail
    except ShareImage.DoesNotExist:
        return False

    new_image = instance.image
    new_thumb = instance.thumbnail

    if old_image != new_image:
        if os.path.isfile(old_image.path):
            os.remove(old_image.path)

    if old_thumb != new_thumb:
        if os.path.isfile(old_thumb.path):
            os.remove(old_thumb.path)

example app2: 示例app2:

import os

from io import BytesIO
from PIL import Image
from django.db import models
from django.dispatch import receiver
from django.core.files.base import ContentFile
from .storage import OverwriteStorage


class Logo(models.Model):
    title = models.CharField(max_length=100, unique=True, verbose_name='Titel')
    slug = models.SlugField(max_length=100, unique=True)
    created = models.DateTimeField(auto_now_add=True, verbose_name='Erstellt')
    updated = models.DateTimeField(auto_now=True, verbose_name='Aktualisiert')
    image = models.ImageField(upload_to='website/header/images/',
                              verbose_name='Bild', storage=OverwriteStorage())
    thumbnail = models.ImageField(upload_to='website/header/images/',
                                  editable=False, storage=OverwriteStorage())
    public = models.BooleanField(verbose_name='Öffentlich')

    class Meta:
        ordering = ['-updated']
        verbose_name = 'Logo'
        verbose_name_plural = 'Logos'

    def __str__(self):
        return self.title

You can make ShareImage an abstract model , and subclass other classes from it. 您可以使ShareImage成为抽象模型 ,并从中ShareImage其他类。 For example: 例如:

class ShareImage(models.Model):
   ...
   class Meta:
        abstract = True


class Logo(ShareImage):
   ...

In that way, Logo class will have fields image and thumbnail . 这样, Logo类将具有imagethumbnail字段。 But that will not trigger the signals. 但这不会触发信号。

Also, there is another way, to add ShareImage as OneToOneField to Logo : 另外,还有另一种方法可以将ShareImage作为OneToOneField添加到Logo

class Logo(models.Model):
    image = models.OneToOneField(ShareImage)
    ...

    def delete(self, *args, **kwargs):
        self.image.delete()
        return super(Logo, self).delete(*args, **kwargs)

In that way, ShareImage deletion should trigger those signals. 这样, ShareImage删除应该触发那些信号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM