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

58 views
Skip to first unread message

Robert Leach

unread,
Oct 29, 2022, 1:28:48 PM10/29/22
to Django users
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
Reply all
Reply to author
Forward
0 new messages