简体   繁体   English

模块化的矮人

[英]Modularizing peewee

Suppose I have a couple of simple models residing in food.py : 假设我有几个驻留在food.py的简单模型:

import peewee as pw

db = pw.SqliteDatabase('food.db')

class BaseModel(pw.Model):
    class Meta:
        database = db

class Taco(BaseModel):
    has_cheese = pw.BooleanField()

class Spaghetti(BaseModel):
    has_meatballs = pw.BooleanField()

db.connect()

# populate with some data if table doesn't exist
from random import random
if not Taco.table_exists():
    db.create_table(Taco)
    for _ in range(10):
        Taco.create( has_cheese = (random() < 0.5) )
    db.commit()
if not Spaghetti.table_exists():
    db.create_table(Spaghetti)
    for _ in range(10):
        Spaghetti.create( has_meatballs = (random() < 0.5) )
    db.commit()

Afterwards, I have food.py and food.db . 之后,我有food.pyfood.db But let's say the Taco and Spaghetti models are becoming large and complicated, so I'd like to split them into different files. 但是,可以说TacoSpaghetti模型正变得庞大而复杂,因此我想将它们拆分为不同的文件。 Specifically, I'd like to create a food folder in my PYTHONPATH with the typical hierarchy: 具体来说,我想在我的PYTHONPATH使用典型的层次结构创建一个food文件夹:

food/
    - __init__.py
    - BaseModel.py
    - Taco.py
    - Spaghetti.py
    - db/
        - food.db

I'd like to put the models into their respective .py files and have an __init__.py file that looks something like this: 我想将模型放入各自的.py文件中,并拥有一个看起来像这样的__init__.py文件:

import peewee as pw

db = pw.SqliteDatabase('./db/food.db')

from . import BaseModel
from . import Taco
from . import Spaghetti

db.connect()

However, this clearly doesn't work because BaseModel.py can't access db . 但是,这显然不起作用,因为BaseModel.py无法访问db If it is possible to modularize multiple peewee models in this manner, what is the correct way to do so? 如果可以这种方式模块化多个皮尤模型,那么正确的方法是什么?

Apparently the trick is to connect to the database in the BaseModel.py file. 显然,诀窍是连接到BaseModel.py文件中的数据库。 I will give a full outline of the module contents. 我将提供模块内容的完整概述。 Assume that the top-level folder is named food and lives in the PYTHONPATH . 假设顶层文件夹名为food并且位于PYTHONPATH Finally assume that food.db exists in food/db/food.db and has been populated (eg, as in the bottom of the very first code block in the question). 最后,假设food.db存在于food/db/food.db并已填充(例如,如问题中第一个代码块的底部所示)。

Here are the module files: 以下是模块文件:

__init__.py __init__.py

from Taco import Taco
from Spaghetti import Spaghetti

BaseModel.py BaseModel.py

import peewee as pw
db = pw.SqliteDatabase('/abs/path/to/food/db/food.db')

class BaseModel(pw.Model):
    class Meta:
        database = db

Taco.py Taco.py

import peewee as pw
from BaseModel import BaseModel

class Taco(BaseModel):
    has_cheese = pw.BooleanField()

Spaghetti.py Spaghetti.py

import peewee as pw
from BaseModel import BaseModel

class Spaghetti(BaseModel):
    has_meatballs = pw.BooleanField()

Now, for example, you can write a script (residing outside the module folder, of course), like: 现在,例如,您可以编写一个脚本(当然位于模块文件夹的外部),例如:

main.py main.py

import food

for t in food.Taco.select():
    print "Taco", t.id, ("has" if t.has_cheese else "doesn't have"), "cheese"

produces: 生产:

Taco 1 has cheese
Taco 2 has cheese
Taco 3 has cheese
Taco 4 doesn't have cheese
Taco 5 doesn't have cheese
Taco 6 has cheese
Taco 7 has cheese
Taco 8 has cheese
Taco 9 doesn't have cheese
Taco 10 doesn't have cheese

You have a problem in the path: 您在路径中有问题:

__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
db = pw.SqliteDatabase(os.path.join(__location__, 'db/food.db'));

Try also to implement __init__ of classes and pass the db as an argument to it: 还尝试实现类的__init__并将db作为参数传递给它:

class BaseModel(pw.Model):
    def __init__(self, db = None)
        self.database = db

than in __init__.py : __init__.py

from BaseModel import BaseModel
db = pw.SqliteDatabase('./db/food.db')
bm = BaseModel(db)

See this post for instructions on modularizing a flask app using peewee: 有关使用peewee模块化flask应用程序的说明,请参见此帖子:

http://charlesleifer.com/blog/structuring-flask-apps-a-how-to-for-those-coming-from-django/ http://charlesleifer.com/blog/structuring-flask-apps-a-how-to-for-those-coming-from-django/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM