Question

I have a simple website made with Flask and Peewee as the ORM that annotates the drinks everyone in the office gets to then, once a week or so, check who drink what, how much and when. I have a simple table named orders that looks like this:

Orders - Id, product, user, amount, price, date

And what I want to do is have a simple page where someone can consult what happened from a specific date to the current day (For example, from 14-02 to today 21-02). How do I make such query in peewee?

Was it helpful?

Solution

As someone who never heard of Peewee until ten minutes ago, take this with a grain of salt:

from datetime import date
from peewee import *

class DrinksModel(Model):
    class Meta:
        database = SqliteDatabase('drinks.db')

class Order(DrinksModel):
    id      = PrimaryKeyField()
    product = CharField()
    user    = CharField()
    amount  = IntegerField()
    price   = DecimalField()
    date    = DateField()

def get_orders_since(year, month=1, day=1):
    orders = Order.select().where(Order.date >= date(year, month, day))

for order in get_orders_since(2014, 2, 14):
    print(
        "{}: {} drank {} x {}"
        .format(order.date, order.user, order.amount, order.product)
    )

If this gives an error, let me know what it says and we'll go from there.

Suggested changes:

  1. The user field should probably become a foreign key into a User table

  2. The product field should become a foreign key into a Product table, and the price field should go into the Product table

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top