创建;“查询方法”;在Peewee模型中,Python
我在一个flask项目中使用Peewee for MySQL连接。我想知道是否有可能在模型的方法中进行查询。这将使路线代码更清晰,例如: Person.py:创建;“查询方法”;在Peewee模型中,Python,python,flask,peewee,Python,Flask,Peewee,我在一个flask项目中使用Peewee for MySQL连接。我想知道是否有可能在模型的方法中进行查询。这将使路线代码更清晰,例如: Person.py: from peewee import * db = SqliteDatabase('people.db') class Person(Model): name = CharField() birthday = DateField() is_relative = BooleanField() class
from peewee import *
db = SqliteDatabase('people.db')
class Person(Model):
name = CharField()
birthday = DateField()
is_relative = BooleanField()
class Meta:
database = db # This model uses the "people.db" database.
def getPersonByName(name):
#logic to get persons by name
#return result
Server.py:
.
.
.
@app.route('/<name>')
def index(name):
persons = Person.getPersonByName(name)
return render_template('names.html', persons=persons)
。
.
.
@应用程序路径(“/”)
def索引(名称):
persons=Person.getPersonByName(名称)
返回呈现模板('names.html',persons=persons)
可以向模型中添加自定义方法以进行查询,从而使模型保持胖,视图保持瘦
class Person(Model):
...
@classmethod
def get_person_by_name(cls, name):
result = cls.select().where(cls.name == name)
return result
@app.route('/<name>')
def index(name):
persons = Person.get_person_by_name(name)
return render_template('names.html', persons=persons)
班级人员(模型):
...
@类方法
def按姓名获取人员(cls,姓名):
结果=cls.select()。其中(cls.name==name)
返回结果
@应用程序路径(“/”)
def索引(名称):
persons=Person。通过名称(name)获取Person
返回呈现模板('names.html',persons=persons)
可以向模型中添加自定义方法以进行查询,从而使模型保持胖,视图保持瘦
class Person(Model):
...
@classmethod
def get_person_by_name(cls, name):
result = cls.select().where(cls.name == name)
return result
@app.route('/<name>')
def index(name):
persons = Person.get_person_by_name(name)
return render_template('names.html', persons=persons)
班级人员(模型):
...
@类方法
def按姓名获取人员(cls,姓名):
结果=cls.select()。其中(cls.name==name)
返回结果
@应用程序路径(“/”)
def索引(名称):
persons=Person。通过名称(name)获取Person
返回呈现模板('names.html',persons=persons)