Skip to content

Commit

Permalink
Attr-cache the base model class exposed by db.Model property.
Browse files Browse the repository at this point in the history
  • Loading branch information
coleifer committed Oct 26, 2022
1 parent 3ca1885 commit 2d7ed54
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions peewee.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ class DatabaseProxy(Proxy):
"""
Proxy implementation specifically for proxying `Database` objects.
"""
__slots__ = ('obj', '_callbacks', '_Model')

def connection_context(self):
return ConnectionContext(self)
def atomic(self, *args, **kwargs):
Expand All @@ -476,8 +478,10 @@ def savepoint(self):
return _savepoint(self)
@property
def Model(self):
class Meta: database = self
return type('BaseModel', (Model,), {'Meta': Meta})
if not hasattr(self, '_Model'):
class Meta: database = self
self._Model = type('BaseModel', (Model,), {'Meta': Meta})
return self._Model


class ModelDescriptor(object): pass
Expand Down Expand Up @@ -3455,8 +3459,10 @@ def get_noop_select(self, ctx):

@property
def Model(self):
class Meta: database = self
return type('BaseModel', (Model,), {'Meta': Meta})
if not hasattr(self, '_Model'):
class Meta: database = self
self._Model = type('BaseModel', (Model,), {'Meta': Meta})
return self._Model


def __pragma__(name):
Expand Down

0 comments on commit 2d7ed54

Please sign in to comment.