-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
42 lines (33 loc) · 1.15 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import datetime
from peewee import *
db = SqliteDatabase('my_app.db')
class BaseModel(Model):
class Meta:
database = db
class User(BaseModel):
name = CharField(unique=True)
address = CharField()
billing_information = CharField()
class Product(BaseModel):
user = ForeignKeyField(User, backref='products')
name = CharField()
description = TextField()
price = DecimalField(max_digits=10, decimal_places=2)
quantity = IntegerField()
class Tag(BaseModel):
name = CharField(unique=True)
class ProductTag(BaseModel):
product = ForeignKeyField(Product, backref='product_tags')
tag = ForeignKeyField(Tag, backref='product_tags')
class Meta:
# A product can't have duplicated tags
indexes = (
# create a unique on product/tag
(('product', 'tag'), True),
)
class Transaction(BaseModel):
buyer = ForeignKeyField(User, backref='purchases')
product = ForeignKeyField(Product, backref='transactions')
quantity = IntegerField()
timestamp = DateTimeField(default=datetime.datetime.now)
db.create_tables([User, Product, Tag, ProductTag, Transaction], safe=True)