from django.contrib.contenttypes.models import ContentType from django.db.models import QuerySet class PhotoableModel: """Mixin for models that can have photos attached.""" def get_photos(self) -> QuerySet: """Get photos for this instance.""" from media.models import Photo ct = ContentType.objects.get_for_model(self.__class__) return Photo.objects.filter(content_type=ct, object_id=self.pk) def add_photo(self, photo: 'Photo') -> None: """Add a photo to this instance.""" from media.models import Photo ct = ContentType.objects.get_for_model(self.__class__) photo.content_type = ct photo.object_id = self.pk photo.save()