Saturday, October 29, 2022

How to override Model.__init__ without breaking `using(db)`?

Hi,

I have a Model superclass called `MaintainedModel` that other models inherit from.  It itself inherits from django.db.models.Model and overrides its __init__ method in order to implement some controls on fields using decorators in the derived models.  It all works very well in the realm of the default database... BUT I realized yesterday that it breaks operations on our validation database.  I describe it in some detail on this stack post, but to summarize my hunch, the override of init is breaking the `.using(db)` statement in this call:

```
FCirc.objects.using(self.db).get_or_create(
    serum_sample=sample,
    tracer=tracer,
    element=label.element,
)
```

As a proof of concept, I fix that broken association in my "answer" on the stack post by axplicitly setting a `_state` attribute on the FCirc object before calling `get_or_create` and that fix works.

So my question for you all is: how do I preserve the functionality of `.using(db)` in my MaintainedModel superclass?

Thanks,
Rob

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5767c6e1-7636-462d-a8cc-5f819350f755n%40googlegroups.com.

No comments:

Post a Comment