peewee

Can peewee nest SELECT queries such that the outer query selects on an aggregate of the inner query?

会有一股神秘感。 提交于 2020-01-01 19:06:33
问题 I'm using peewee2.1 with python3.3 and an sqlite3.7 database. I want to perform certain SELECT queries in which: I first select some aggregate (count, sum), grouping by some id column; then I then select from the results of (1), aggregating over its aggregate. Specifically, I want to count the number of rows in (1) that have each aggregated value. My database has an 'Event' table with 1 record per event, and a 'Ticket' table with 1..N tickets per event. Each ticket record contains the event's

Peewee and Flask : 'Database' object has no attribute 'commit_select'

梦想与她 提交于 2019-12-25 00:34:34
问题 I'm trying to use Peewee with Flask, but I don't understand why my database connection does not work. config.py class Configuration(object): DATABASE = { 'name': 'test', 'engine': 'peewee.MySQLDatabase', 'user': 'root', 'passwd': 'root' } DEBUG = True SECRET_KEY = 'shhhh' app/ init .py from flask import Flask from flask_peewee.db import Database app = Flask(__name__) app.config.from_object('config.Configuration') db = Database(app) import views,models app/models.py from peewee import * from .

How to update multiple records using peewee

廉价感情. 提交于 2019-12-23 15:12:01
问题 I'm using Peewee with Postgres database. I want to know how to update multiple records in a tabel at once? We can perform this update in SQL using these commands, and I'm looking for a Peewee equivalent approach. 回答1: ORMs usually dose not support bulk update and you have to use custom SQL, you can see samples in this link (db.excute_sql) 回答2: Yes, you can use the insert_many() function: Insert multiple rows at once. The rows parameter must be an iterable that yields dictionaries. As with

In Peewee I have a datetime field defaulted to datetime.datetime.now(). But when inserted, it takes the time the server was started. Why

半城伤御伤魂 提交于 2019-12-23 12:10:05
问题 When I insert a row, the field is filled with the time when the server was started not the time when the row was inserted. Why is this happening and what is the solution? BTW I am using SQLite. class LOG(peewee.Model): id = peewee.IntegerField(unique=True,primary_key=True) timestamp = peewee.DateTimeField(default=datetime.datetime.now()) log = peewee.CharField() by = peewee.IntegerField(default=1) class Meta: database = database LOG.create(log = _log , by = _by) # above statement is called at

How to query several similar databases using Peewee?

喜你入骨 提交于 2019-12-22 17:49:02
问题 I'm stuck with a problem about querying multiple databases using Peewee: I have 2 existing mysql databases (let's name them A and B) (Structures are similar because it's two Bugzilla databases) I generate models (modelsA.py and modelsB.py) using Pwiz I write this code: . from modelsA import * from modelsB import * The problem is: as the classes in modelsB are (sometimes) the same than modelsA, modelsB classes "overwrite" modelsA classes, making impossible to query A. Besides, I don't

Make primary-key fields editable in Flask-Admin

隐身守侯 提交于 2019-12-22 10:06:52
问题 I am using Flask-Admin for my Flask-based project. In it, I have some models (using peewee) where the primary-key is user-set, such as username for a User . However Flask-Admin is not showing these fields in the model's create/edit pages. Now, when I try to create a new user, the "Save" button gives a peewee.UserDoesNotExist error, and the "Save & Add" says "Record successfully created" twice but doesn't actually do anything. I had extended the save() method to auto-generate the username from

Python and Postgresql: OperationalError: fe_sendauth: no password supplied

做~自己de王妃 提交于 2019-12-22 09:47:34
问题 I know there are a lot of similar questions on StackOverflow, but I have read and re-read them, and I cannot seem to solve my particular issue. I am developing a Python application that uses Peewee and Psycopg2 to access PostGresQL databases. This is all being run in an Ubuntu Vagrant Virtual Machine. I keep getting this error when I try to add a user via Python: peewee.OperationalError: fe_sendauth: no password supplied Here is the code where I try to add a user : def add_user(user, password

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

ぃ、小莉子 提交于 2019-12-20 11:36:26
问题 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 set the Column options in MySQL db? 回答1: You can override the save method on your model class. class Something(Model): created = DateTimeField(default=datetime.datetime.now) modified = DateTimeField def save(self, *args, **kwargs): self.modified =

peewee.OperationalError: too many SQL variables on upsert of only 150 rows * 8 columns

你说的曾经没有我的故事 提交于 2019-12-14 02:16:31
问题 With the below example, on my machine, setting range(150) leads to the error, while range(100) does not: from peewee import * database = SqliteDatabase(None) class Base(Model): class Meta: database = database colnames = ["A", "B", "C", "D", "E", "F", "G", "H"] cols = {x: TextField() for x in colnames} table = type('mytable', (Base,), cols) database.init('test.db') database.create_tables([table]) data = [] for x in range(150): data.append({x: 1 for x in colnames}) with database.atomic() as txn

Dynamically define name of class in peewee model

三世轮回 提交于 2019-12-13 23:51:40
问题 I'm trying to assign a class name dynamically using a string. Much like this... classname='cats' class classname(peewee.Model): Peewee doesn't seem to think I should be able to do this and I'm having a lot of trouble finding a way to define the class name dynamically. Help! 回答1: If you need to control the table name, you can do: class MyModel(Model): whatever = CharField() class Meta: db_table = 'my_table_name' 回答2: I have a same problem with you,and find a solution at last. Maybe it's not a