Questions tagged [peewee]

peewee is a small ORM written in Python, providing a lightweight querying interface over SQL.

peewee is a small ORM written in Python. It provides a lightweight querying interface over SQL, and uses SQL concepts when querying, like joins and where clauses. It supports sqlite, mysql and postgresql.

1197 questions
35
votes
7 answers

Peewee model to JSON

I'm creating an API using peewee as the ORM and I need the ability to convert a peewee model object into a JSON object to send to the user. Does anyone know of a good way to do this?
shoopdelang
  • 985
  • 2
  • 9
  • 20
27
votes
1 answer

Accessing remote MySQL database with peewee

I'm trying to connect to a MySQL database on Amazon's RDS using peewee and I can't get it to work. I'm new to databases so I'm probably doing something stupid, but this is what I'm trying: import peewee as pw myDB =…
Alex S
  • 4,726
  • 7
  • 39
  • 67
24
votes
1 answer

Peewee syntax for selecting on null field

I have researched this everywhere and can't seem to find an answer. I hope I haven't duplicated this (as it's my first question on SO). I am trying to write a select query with Peewee that would normally go ... WHERE foo = NULL; in SQL world. MySQL…
thaavik
  • 3,257
  • 2
  • 18
  • 25
24
votes
4 answers

is there an auto update option for DateTimeField in peewee like TimeStamp in MySQL?

I would like a timestamp field updating each time the record is modified like in MySQL. DateTimeField(default=datetime.datetime.now()) will only set it the first time it is created... Any have a simple solution? Is the only solution is to manually…
Xelt
  • 406
  • 3
  • 9
22
votes
2 answers

How to do query with `WHERE value IN list` in the Python Peewee ORM?

I'm using the (awesome) Python Peewee ORM for my Flask project, but I now got stuck trying to do a query with a where value in ['a', 'b', 'c']. I tried doing it as follows: MyModel.select().where(MyModel.sell_currency in ['BTC', 'LTC']) But…
kramer65
  • 50,427
  • 120
  • 308
  • 488
19
votes
1 answer

Python Peewee execute_sql() example

I am using the Peewee module as the ORM for my project. I read the entire documentation, there is no clear example on how to process the result from db.execute_sql(). I traced the code, only can find db.execute_sql() return back the cursor. Does…
user1187968
  • 7,154
  • 16
  • 81
  • 152
16
votes
3 answers

How to convert a select query into a pandas DataFrame using PeeWee

Using the PeeWee ORM I have the following query: query = DataModel.select()where(DataModel.field == "value") Is there any way to convert query into a pandas DataFrame without iterating over all the values? I'm looking for a more "Pythonic" way of…
MikeyE
  • 1,756
  • 1
  • 18
  • 37
16
votes
3 answers

Operator NOT IN with Peewee

The documentation shows here how to use the IN operator, but I couldn't find how to use the NOT IN operator. If I put a not << I get a syntax error. If I put a not << there is a WHERE False instead of a subquery like WHERE (
stenci
  • 8,290
  • 14
  • 64
  • 104
15
votes
3 answers

check if query exists using peewee

I am using the Peewee library in Python and I want to check if a query exists. I do not want to create a record if it doesn't exist, so I don't want to use get_or_create. There must be a better solution than to use try/except with get but I don't…
calthoff
  • 386
  • 1
  • 4
  • 14
15
votes
1 answer

python peewee - how to use distinct

I'm trying to get this code working with peewee: distinct_list = QSales.select(QSales.account, QSales.tax_code).distinct().where(QSales.trans_num == 3717) print distinct_list but the print command result is: SELECT…
Erans
  • 401
  • 1
  • 5
  • 11
14
votes
1 answer

Insert or update a peewee record in python

Is there a simple one-line way using peewee in Python of either inserting a record if the primary key does not already exist, or updating the record if it does already exist. Currently i am using the code: try: sql_database.create(record_key =…
kyrenia
  • 5,431
  • 9
  • 63
  • 93
14
votes
6 answers

How to get columns/fields with peewee query?

For a model class User(db.Model, BaseUser): name = CharField() phone = CharField() age = IntegerField() points = IntegerField() and a list of fields, lst = ['phone', 'name', 'points'] Is there a way to get your query to return the…
beardc
  • 20,283
  • 17
  • 76
  • 94
13
votes
1 answer

Get ordered field names from peewee Model

I'd like to use peewee to create records from a csv. It looks like the syntax requires keyword args: user = User.create(username='admin', password='test') If the rows in the csv look like (admin, test), it would be convenient to know the field…
beardc
  • 20,283
  • 17
  • 76
  • 94
13
votes
4 answers

Why is peewee including the 'id' column into the mysql select query?

I am trying to learn how to use peewee with mysql. I have an existing database on a mysql server with an existing table. The table is currently empty (I am just testing right now). >>> db = MySQLDatabase('nhl', user='root', passwd='blahblah') >>>…
codingknob
  • 11,108
  • 25
  • 89
  • 126
12
votes
3 answers

Return single peewee record as dict

If I'm getting multiple records from a database with peewee, I can convert them to dicts like this: users = User.select().where(User.attribute == some_value).dicts() However, often I only want one record (or know that only one record will be…
thosphor
  • 2,493
  • 7
  • 26
  • 42
1
2 3
79 80