Optimized Model instantiation a bit.

* Avoid some unnecessary attribute lookups, e.g. access signals directly rather than from module
* Alias some repeat accesses inside the method to use the slightly faster local lookups
* Use tuple to iterate remaining kwargs as it's faster to construct
* Cache Field.get_default() to avoid running through all the logic on every call
* Use a cached list of the properties on the model class to avoid repeat isinstance() calls
This commit is contained in:
Adam Chainz 2016-12-15 18:42:44 +00:00 committed by Tim Graham
parent f94475e526
commit d2a26c1a90
3 changed files with 60 additions and 28 deletions

View file

@ -883,3 +883,14 @@ class Options(object):
@has_auto_field.setter
def has_auto_field(self, value):
pass
@cached_property
def _property_names(self):
"""
Return a set of the names of the properties defined on the model.
Internal helper for model initialization.
"""
return frozenset({
attr for attr in
dir(self.model) if isinstance(getattr(self.model, attr), property)
})