Saturday, October 29, 2022

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


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:


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?


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
To view this discussion on the web visit

No comments:

Post a Comment