1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] How to use a post_save receiver with django-polymorphic?

Discussão em 'Python' iniciado por Stack, Setembro 12, 2024.

  1. Stack

    Stack Membro Participativo

    I have a django-polymorphic model and want to implement a post_save signal to automatically create a related model that is also polymorphic.

    It's something like the code below, the relevant piece of non-working code being the @receiver decorated work_post_save method. The problem is the instance is a ctype_id and not an object.

    from django.db import models
    from django.db.models import signals
    from django.dispatch import receiver
    from polymorphic.models import PolymorphicModel

    from mygallery.models import Album

    # Work Parent PolymorphicModel
    class Work(PolymorphicModel):
    title = models.CharField(blank=True,max_length=256)
    slug = models.SlugField(max_length=256)

    @receiver(signals.post_save, sender=Work)
    def work_post_save(sender, instance, signal, created, **kwargs):
    album, new = Album.objects.get_or_create(title=instance.title + ' Stills', slug=instance.slug + '-stills')
    work_album, new = WorkAlbum.objects.get_or_create(work=instance, album=album, is_key=True)


    class ArtProject(Work):
    manifesto = models.CharField(blank=True,max_length=256)

    class CodeProject(Work):
    code = models.CharField(blank=True,max_length=256)


    # Content Parent PolymorphicModel
    class WorkContent(PolymorphicModel):
    is_key = models.BooleanField(default=False, unique=True, default=False)


    class WorkAlbum(WorkContent):
    work = models.ForeignKey(Work, related_name='work_albums')
    album = models.ForeignKey(Album, related_name='album_works')

    Continue reading...

Compartilhe esta Página