Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建;“查询方法”;在Peewee模型中,Python_Python_Flask_Peewee - Fatal编程技术网

创建;“查询方法”;在Peewee模型中,Python

创建;“查询方法”;在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

我在一个flask项目中使用Peewee for MySQL连接。我想知道是否有可能在模型的方法中进行查询。这将使路线代码更清晰,例如:

Person.py:

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)