Peewee and Flask : Database object has no attribute commit_select

2024/10/14 4:29:03

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 . import db
database = dbclass UnknownField(object):def __init__(self, *_, **__): pass
class BaseModel(Model):class Meta:database = databaseclass Tbcategory(BaseModel):insert_dt = DateTimeField()name = CharField()class Meta:db_table = 'tbcategory'

I generated models.py with pwiz.

If I try to use it on the interactive console I get the error on the title. If I change the line on models.py from database=db to the original one created by pwiz:

db = MySQLDatabase('test', **{'host': '127.0.0.1', 'password': 'root', 'user': 'root'})

everything works fine. I can't find for the life of me an example on internet. Either the configuration is all in the app or it's outside in a config.py file but with sqlite or some orther slightly different usages. Should I stop using Database() from flask_peewee and using the MySQLDatabase directly? How do i use an external file with the configuration? Note that I use 127.0.0.1 on one method, and no host specification on the other. I did copy from peewee-flask's website.

Answer

You are using the Database wrapper object. To get at the actual Peewee database object, use:

app.config.from_object('config.Configuration') 
db = Database(app)
database = db.database  # database is the actual peewee database obj.

In your models code:

from peewee import *
from . import database  # Import the actual peewee database
https://en.xdnf.cn/q/117998.html

Related Q&A

for loop to create a matrix in python

I am trying to study the probability of having a zero value in my data and I have developed a code that outputs the value of a column of data when the other is zero which is what I need. But having to …

How to convert List of JSON frames to JSON frame

I want to convert List of JSON object ot Single JSON frameHere is my codefor i in user1:name=i.namepassword=i.passwordid1=i.iduser = { "name" : name,"password" : password,"id&q…

Python 2.7 The packaging package is required; normally this is bundled with this package

I expect this has to do with the cryptography module, but Im not sure.Traceback (most recent call last):File "<string>", line 11, in <module>File "c:\python27\lib\site-packag…

Couple the data in all possible combinations

I have data in column in two columns like thisId Value 1 a 2 f 1 c 1 h 2 aand Id like couple the data of the Value column in all possible combinations based on the same Id such as(a,c) (a,h)…

Python - Find date from string

Would anyone know a regex string or another method of obtaining the date and time from this string into variables? The position of the string could change, so line and char no would not work. This is …

Get 1st column values on .csv file on python

i am newbie at python programming, i have a .csv file containing mac address and signal strength data from an AP consider my csv data is:i want to get just mac address values which is the 1st row, ref…

how to click mouse over sub menu in selenium?

I want to click invisible htmls sub menu click.*invisible html source<ul class="options"> <li class="_ranking-attr-filter-container _sub-menu-target"> <span>Hide w…

SSL: CERTIFICATE_VERIFY_FAILED certificate verify failed (_ssl.c.661)

Im trying to install nltk on my Mac, but I keep getting this error message after following these instructions: Install NLTK: sudo pip install -U nltk Install Numpy (optional): sudo pip install -U numpy…

Real-time reading of terminal output from server

Im trying to process images from my camera on my server and get the information after processing on my local machine in real-time. I can get necessary information as terminal outputs on my server, but …

Transform map to mapPartition using pyspark

I am trying to load a tensorflow model from disk and predicting the values.Codedef get_value(row):print("**********************************************")graph = tf.Graph()rowkey = row[0]check…