Model Definitions
To begin working with the data, we’ll define the model classes that correspondto the tables in the diagram.
Note
In some cases we explicitly specify column names for a particular field.This is so our models are compatible with the database schema used for thepostgres exercises.
- from functools import partial
- from peewee import *
- db = PostgresqlDatabase('peewee_test')
- class BaseModel(Model):
- class Meta:
- database = db
- class Member(BaseModel):
- memid = AutoField() # Auto-incrementing primary key.
- surname = CharField()
- firstname = CharField()
- address = CharField(max_length=300)
- zipcode = IntegerField()
- telephone = CharField()
- recommendedby = ForeignKeyField('self', backref='recommended',
- column_name='recommendedby', null=True)
- joindate = DateTimeField()
- class Meta:
- table_name = 'members'
- # Conveniently declare decimal fields suitable for storing currency.
- MoneyField = partial(DecimalField, decimal_places=2)
- class Facility(BaseModel):
- facid = AutoField()
- name = CharField()
- membercost = MoneyField()
- guestcost = MoneyField()
- initialoutlay = MoneyField()
- monthlymaintenance = MoneyField()
- class Meta:
- table_name = 'facilities'
- class Booking(BaseModel):
- bookid = AutoField()
- facility = ForeignKeyField(Facility, column_name='facid')
- member = ForeignKeyField(Member, column_name='memid')
- starttime = DateTimeField()
- slots = IntegerField()
- class Meta:
- table_name = 'bookings'