models module

class models.User(*args, **kwargs)

Bases: pymodm.base.models.MongoModel

exception DoesNotExist

Bases: pymodm.errors.DoesNotExist

exception MultipleObjectsReturned

Bases: pymodm.errors.MultipleObjectsReturned

age

A field that stores a Python int.

email

A field that stores email addresses.

heart_rate

A field that stores a list.

heart_rate_times

A field that stores a list.

objects

The default manager used for MongoModel instances.

This implementation of BaseManager uses QuerySet as its QuerySet class.

This Manager class (accessed via the objects attribute on a MongoModel) is used by default for all MongoModel classes, unless another Manager instance is supplied as an attribute within the MongoModel definition.

Managers have two primary functions:

  1. Construct QuerySet instances for use when querying or working with MongoModel instances in bulk.
  2. Define collection-level functionality that can be reused across different MongoModel types.

If you created a custom QuerySet that makes certain queries easier, for example, you will need to create a custom Manager type that returns this queryset using the from_queryset() method:

class UserQuerySet(QuerySet):
    def active(self):
        '''Return only active users.'''
        return self.raw({"active": True})

class User(MongoModel):
    active = fields.BooleanField()
    # Add our custom Manager.
    users = Manager.from_queryset(UserQuerySet)

In the above example, we added a users attribute on User so that we can use the active method on our new QuerySet type:

active_users = User.users.active()

If we wanted every method on the QuerySet to examine active users only, we can do that by customizing the Manager itself:

class UserManager(Manager):
    def get_queryset(self):
        # Override get_queryset, so that every QuerySet created will
        # have this filter applied.
        return super(UserManager, self).get_queryset().raw(
            {"active": True})

class User(MongoModel):
    active = fields.BooleanField()
    users = UserManager()

active_users = User.users.all()