peewee-async icon indicating copy to clipboard operation
peewee-async copied to clipboard

Manager for each model

Open dennypenta opened this issue 8 years ago • 1 comments

May I create Manager for each my model? Or that would work not good?

For example:

class Model(peewee.Model):
  _objects = None

  @property
  def objects(self):
    if self._objects:
      return self._objects
    self._objects = peewee_async.Manager(self._meta.database)
    return self._objects

So if I wanna do something like this, should I use Model.objects.close() explicity for ever query to database?

dennypenta avatar Sep 23 '17 16:09 dennypenta

I don't think so, but you would need to access objects through Model object not class, and it would create model per object

I recently refactored code in our project to this:

class _ClassProperty(property):
    def __init__(self, fget, *arg, **kw):
        super(classproperty, self).__init__(fget, *arg, **kw)
        self.__doc__ = fget.__doc__

    def __get__(self, obj, cls):
        return self.fget(cls)

classproperty = _ClassProperty


class BaseModel(peewee.Model):
    @classproperty
    def manager(cls):  # noqa: N805
        return peewee_async.Manager(cls._meta.database)

    class Meta:
        abstract = True

No other changes were required.

serathius avatar Sep 26 '17 13:09 serathius