peewee: 对象没有属性 _meta

3 投票
1 回答
3849 浏览
提问于 2025-04-17 20:08

我在Windows上用Python 3.3的32位版本工作。我已经安装了peewee,并想尝试它的一些功能。我从Peewee的快速入门开始学习,链接在这里:http://peewee.readthedocs.org/en/latest/peewee/quickstart.html

我的代码是这样的:

from peewee import *

db = SqliteDatabase('people.db')

class Person(Model):
    name = CharField()
    birthday = DateField()
    is_relative = BooleanField()

    class Meta:
            database = db

class Pet(Model):
    owner = ForeignKeyField(Person, related_name = "pets")
    name = CharField()
    animal_type = CharField()

    class Meta:
            database = db

Person.create_table()
Pet.create_table()

但是我遇到了一个错误:

File "<stdin>", line 1, in <module>
File "<string>", line 21, in <module>
File "C:\Python33\lib\site-packages\peewee.py", line 2094, in create_table
db = cls._meta.database
AttributeError: type object 'Person' has no attribute '_meta'

我的peewee安装有问题吗?我该如何解决这个问题呢?

1 个回答

5

Peewee 目前支持 Python 3,只能在 Python 2 上使用。

你看到的错误就是因为这个原因;Model 类使用了 Python 2 的一些技术来定义元类,而这些在 Python 3 中已经改变了。

更新2.1 版本于 2013 年 4 月 2 日发布,增加了对 Python 3 的支持。现在这个包可以在 Python 2.6、2.7 以及 3.2 及以上版本中使用。

撰写回答