How to use Django model inheritance with signals?

前端 未结 8 787
无人共我
无人共我 2020-12-23 11:15

I have a few model inheritance levels in Django:

class WorkAttachment(models.Model):
    \"\"\" Abstract class that holds all fields that are required in eac         


        
8条回答
  •  死守一世寂寞
    2020-12-23 11:55

    You could try something like:

    model_classes = [WorkAttachment, WorkAttachmentFileBased, WorkAttachmentPicture, ...]
    
    def update_attachment_count_on_save(sender, instance, **kwargs):
        instance.work.attachment_count += 1
        instance.work.save()
    
    for model_class in model_classes:
        post_save.connect(update_attachment_count_on_save, 
                          sender=model_class, 
                          dispatch_uid="att_post_save_"+model_class.__name__)
    

    (Disclaimer: I have not tested the above)

提交回复
热议问题