简体   繁体   中英

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. is it possible to connect the other apps so that they use the ImageFields of the app1[images] ShareImage class?

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:

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.

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:

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. For example:

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


class Logo(ShareImage):
   ...

In that way, Logo class will have fields image and thumbnail . But that will not trigger the signals.

Also, there is another way, to add ShareImage as OneToOneField to 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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