Model options and table metadata

In order not to pollute the model namespace, model-specific configuration is placed in a special class called Meta (a convention borrowed from the django framework):

  1. from peewee import *
  2. contacts_db = SqliteDatabase('contacts.db')
  3. class Person(Model):
  4. name = CharField()
  5. class Meta:
  6. database = contacts_db

This instructs peewee that whenever a query is executed on Person to use the contacts database.

Note

Take a look at the sample models - you will notice that we created a BaseModel that defined the database, and then extended. This is the preferred way to define a database and create models.

Once the class is defined, you should not access ModelClass.Meta, but instead use ModelClass._meta:

  1. >>> Person.Meta
  2. Traceback (most recent call last):
  3. File "<stdin>", line 1, in <module>
  4. AttributeError: type object 'Person' has no attribute 'Meta'
  5. >>> Person._meta
  6. <peewee.ModelOptions object at 0x7f51a2f03790>

The ModelOptions class implements several methods which may be of use for retrieving model metadata (such as lists of fields, foreign key relationships, and more).

  1. >>> Person._meta.fields
  2. {'id': <peewee.PrimaryKeyField object at 0x7f51a2e92750>,
  3. 'name': <peewee.CharField object at 0x7f51a2f0a510>}
  4. >>> Person._meta.primary_key
  5. <peewee.PrimaryKeyField object at 0x7f51a2e92750>
  6. >>> Person._meta.database
  7. <peewee.SqliteDatabase object at 0x7f519bff6dd0>

There are several options you can specify as Meta attributes. While most options are inheritable, some are table-specific and will not be inherited by subclasses.

OptionMeaningInheritable?
databasedatabase for modelyes
table_namename of the table to store datano
table_functionfunction to generate table name dynamicallyyes
indexesa list of fields to indexyes
primary_keya CompositeKey instanceyes
constraintsa list of table constraintsyes
schemathe database schema for the modelyes
only_save_dirtywhen calling model.save(), only save dirty fieldsyes
optionsdictionary of options for create table extensionsyes
table_aliasan alias to use for the table in queriesno
depends_onindicate this table depends on another for creationno
without_rowidindicate table should not have rowid (SQLite only)no

Here is an example showing inheritable versus non-inheritable attributes:

  1. >>> db = SqliteDatabase(':memory:')
  2. >>> class ModelOne(Model):
  3. ... class Meta:
  4. ... database = db
  5. ... table_name = 'model_one_tbl'
  6. ...
  7. >>> class ModelTwo(ModelOne):
  8. ... pass
  9. ...
  10. >>> ModelOne._meta.database is ModelTwo._meta.database
  11. True
  12. >>> ModelOne._meta.table_name == ModelTwo._meta.table_name
  13. False

Meta.primary_key

The Meta.primary_key attribute is used to specify either a CompositeKey or to indicate that the model has no primary key. Composite primary keys are discussed in more detail here: Composite primary keys.

To indicate that a model should not have a primary key, then set primary_key = False.

Examples:

  1. class BlogToTag(Model):
  2. """A simple "through" table for many-to-many relationship."""
  3. blog = ForeignKeyField(Blog)
  4. tag = ForeignKeyField(Tag)
  5. class Meta:
  6. primary_key = CompositeKey('blog', 'tag')
  7. class NoPrimaryKey(Model):
  8. data = IntegerField()
  9. class Meta:
  10. primary_key = False