from django.db import models from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.models import ContentType from .helpers import get_upload_path class Image(models.Model): image = models.ImageField(upload_to=get_upload_path) title = models.CharField(max_length=128, null=True, blank=True) comment = models.TextField(null=True, blank=True) content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) object_id = models.PositiveIntegerField() content_object = GenericForeignKey('content_type', 'object_id') upload_path = models.CharField(max_length=256, null=True, blank=True) @property def url(self): return self.image.url