#30844: Add after_db_init() hook method to model
-------------------------------------+-------------------------------------
Reporter: rsinger86 | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by rsinger86:
Old description:
> I've encountered a need in my personal projects, and when writing
> [https://github.com/rsinger86/django-lifecycle django-lifecycle], to hook
> into the moment when a model has been fully initialized from the
> database.
>
> Overriding the model's __init__ method does NOT work here because the
> `select_related` relationships have not yet been added in/cached in the
> model's FK fields. It would be useful to do things right after the model
> is fully loaded and initialized from the database. For example, if you
> have a "type" foreign key field, you may want to apply some polymorphic
> behavior based on the value of that field. Doing this in `__init__` will
> cause a n+1 query explosion if you load multiple models and iterate over
> the `QuerySet`.
>
> == Current Problem
> This will cause an n+1 issue when iterating a QuerySet:
> {{{
> class CatMixin(object):
> def greet(self):
> return "Meow"
>
> class DogMixin(object):
> def greet(self):
> return "Woof"
>
> class PolymorphicModel(models.Model):
> type = models.ForeignKey(PetType)
>
> def __init__(self, *args, **kwargs):
> super().__init__(*args, **kwargs)
>
> if type.name == 'cat':
> self.__class__ = CatMixin
> else:
> self.__class__ = DogMixin
> }}}
>
> == The Fix
> Add a call to `obj.post_db_init()` to this line:
> https://github.com/django/django/blob/master/django/db/models/query.py#L91
>
> Then this code will eliminate the n+1 problem (assuming
> `select_related('type')` is used):
> {{{
> class PolymorphicModel(models.Model):
> type = models.ForeignKey(PetType)
>
> def post_db_init(self,):
> if type.name == 'cat':
> self.__class__ = CatMixin
> else:
> self.__class__ = DogMixin
> }}}
>
> I realize there are other ways to achieve the behavior in this example --
> I'm bringing up polymorphism as a general use case. In django-lifecycle,
> this hook would allow me to track a model instance's initial state across
> a foreign key relationship without causing users to experience the n+1
> problem.
New description:
I've encountered a need in my personal projects, and when writing
[https://github.com/rsinger86/django-lifecycle django-lifecycle], to hook
into the moment when a model has been fully initialized from the database.
Overriding the model's __init__ method does NOT work here because the
`select_related` relationships have not yet been added in/cached in the
model's FK fields. It would be useful to do things right after the model
is fully loaded and initialized from the database. For example, if you
have a "type" foreign key field, you may want to apply some polymorphic
behavior based on the value of that field. Doing this in `__init__` will
cause a n+1 query explosion if you load multiple models and iterate over
the `QuerySet`.
== Current Problem
This will cause an n+1 issue when iterating a QuerySet:
{{{
class CatMixin(object):
def greet(self):
return "Meow"
class DogMixin(object):
def greet(self):
return "Woof"
class PolymorphicModel(models.Model):
type = models.ForeignKey(PetType)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# another db query is triggered b/c select_related has populated
the instance yet
if self.type.name == 'cat':
self.__class__ = CatMixin
else:
self.__class__ = DogMixin
}}}
== The Fix
Add a call to `obj.post_db_init()` to this line:
https://github.com/django/django/blob/master/django/db/models/query.py#L91
Then this code will eliminate the n+1 problem (assuming
`select_related('type')` is used):
{{{
class PolymorphicModel(models.Model):
type = models.ForeignKey(PetType)
def post_db_init(self,):
if type.name == 'cat':
self.__class__ = CatMixin
else:
self.__class__ = DogMixin
}}}
I realize there are other ways to achieve the behavior in this example --
I'm bringing up polymorphism as a general use case. In django-lifecycle,
this hook would allow me to track a model instance's initial state across
a foreign key relationship without causing users to experience the n+1
problem.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/30844#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/067.794bf6f06bb6a45372c5fa9a37c5f211%40djangoproject.com.