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):
from peewee import *
contacts_db = SqliteDatabase('contacts.db')
class Person(Model):
name = CharField()
class Meta:
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
:
>>> Person.Meta
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'Person' has no attribute 'Meta'
>>> Person._meta
<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).
>>> Person._meta.fields
{'id': <peewee.PrimaryKeyField object at 0x7f51a2e92750>,
'name': <peewee.CharField object at 0x7f51a2f0a510>}
>>> Person._meta.primary_key
<peewee.PrimaryKeyField object at 0x7f51a2e92750>
>>> Person._meta.database
<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.
Option | Meaning | Inheritable? |
---|---|---|
database | database for model | yes |
table_name | name of the table to store data | no |
table_function | function to generate table name dynamically | yes |
indexes | a list of fields to index | yes |
primary_key | a CompositeKey instance | yes |
constraints | a list of table constraints | yes |
schema | the database schema for the model | yes |
only_save_dirty | when calling model.save(), only save dirty fields | yes |
options | dictionary of options for create table extensions | yes |
table_alias | an alias to use for the table in queries | no |
depends_on | indicate this table depends on another for creation | no |
without_rowid | indicate table should not have rowid (SQLite only) | no |
Here is an example showing inheritable versus non-inheritable attributes:
>>> db = SqliteDatabase(':memory:')
>>> class ModelOne(Model):
... class Meta:
... database = db
... table_name = 'model_one_tbl'
...
>>> class ModelTwo(ModelOne):
... pass
...
>>> ModelOne._meta.database is ModelTwo._meta.database
True
>>> ModelOne._meta.table_name == ModelTwo._meta.table_name
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:
class BlogToTag(Model):
"""A simple "through" table for many-to-many relationship."""
blog = ForeignKeyField(Blog)
tag = ForeignKeyField(Tag)
class Meta:
primary_key = CompositeKey('blog', 'tag')
class NoPrimaryKey(Model):
data = IntegerField()
class Meta:
primary_key = False