Skip to content

Instance attributes missing in post save signal triggered by loaddata #502

@codot-fr

Description

@codot-fr

Hello,

I'm a bit lost. I have created a function to create a celery beat task on a post save signal for child objects of a polymorphic class. When the signal is triggered while django is running, all is working fine. However, when the signal is triggered while loading fixtures (manage.py loaddata), two weird issues arise:

1/ The id field and the pk field of the instance doesn't match
2/ field inherited from the parent polymorphic class aren't set (the name field)

Models:

class Endpoint(PolymorphicModel):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=250, verbose_name="Name")

class GenericSwitch(Endpoint):
    pass

class EXSwitch(GenericSwitch):
    def healthcheck(self):
        pass
@receiver(post_save)
def endpoint_saved_event(sender, instance, created, **kwargs):
    if created and hasattr(instance, 'healthcheck'):
        try:
            logger.info(msg={
                "message": f"Paramétrage automatique de la vérification de l'état de santé de {instance.name} ({instance.__class__}/{instance.id}/{instance.pk})"
            })

When triggered via loaddata, the name attribute is not retrieved and the pk/id fields do not match.

[2021-10-20 11:28:34][core.signals][INFO][None] ::: {'endpoint': {'name': '', 'id': 'f96e3d6e-dc93-4907-aab3-a257536c1282'}, 'message': "Paramétrage automatique de la vérification de l'état de santé de  (<class 'core.models.EXSwitch'>/f96e3d6e-dc93-4907-aab3-a257536c1282/dc04c0ba-a4bb-4704-a440-83329994caac)"}

When triggerd via regular instance create, the name attribute is retrieved and the pk/id fields match.

[2021-10-20 11:44:52][core.signals][INFO][e164139de3aa46a2b65ef34e77cb2acd] ::: {'endpoint': {'name': 'Stack test', 'id': 'ef2a01b3-9f76-49fd-b71d-4d523a483320'}, 'message': "Paramétrage automatique de la vérification de l'état de santé de Stack test (<class 'core.models.EXSwitch'>/ef2a01b3-9f76-49fd-b71d-4d523a483320/ef2a01b3-9f76-49fd-b71d-4d523a483320)"}

Any idea why polymorphic acts differently ?

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions