简体   繁体   English

flask-classy和peewee,元类冲突错误

[英]flask-classy and peewee, metaclass conflict error

I'm trying to get my user class to work with both BaseModel and FlaskView. 我正在尝试让我的用户类与BaseModel和FlaskView一起使用。 This results in the metaclass conflict error and I can't solve it. 这会导致元类冲突错误,而我无法解决。

Things I have tried to fix the problem: 我尝试解决此问题的方法:
This didn't work because of the from noconflict import classmaker . 由于noconflict导入类创建者的影响,这没用 The example is from June 2003. Maybe it is too old? 这个例子是从2003年6月开始的。也许它太旧了? I'm running on python 2.7.3. 我正在python 2.7.3上运行。
http://code.activestate.com/recipes/204197-solving-the-metaclass-conflict/ http://code.activestate.com/recipes/204197-solving-the-metaclass-conflict/

Also tried this solution, see the code blocks below. 也尝试过此解决方案,请参见下面的代码块。 I get this error: AttributeError: type object 'BaseModel' has no attribute ' metaclass ' 我收到此错误:AttributeError:类型对象'BaseModel'没有属性' metaclass '
Double inheritance causes metaclass conflict 双重继承导致元类冲突

from base_model import BaseModel
from flask.ext.classy import FlaskView

class CombinedMeta(BaseModel.__metaclass__, FlaskView.__metaclass__):
    pass


from peewee import *
#sqlite is used for easy testing.
mysql_db = SqliteDatabase('test.db')


class BaseModel(Model):

    class Meta:
        database = mysql_db


from combined_meta import CombinedMeta
from base_model import BaseModel
from flask.ext.classy import FlaskView
from flask.ext.classy import route
from peewee import *
from flask import request
from utility import response_json
from utility import send_email
from utility import random_string

class User(BaseModel, FlaskView):
    __metaclass__ = CombinedMeta

    @route('/<username>', methods=['GET'])
    def read_user(self, username):
        #cool method stuff

When I change the BaseModel class to the following code I get a new error. 当我将BaseModel类更改为以下代码时,出现新错误。
class BaseModel(Model): TypeError: Error when calling the metaclass bases this constructor takes no arguments class BaseModel(Model):TypeError:调用元类基础时出错,此构造方法不接受任何参数

from peewee import *
#sqlite is used for easy testing.
mysql_db = SqliteDatabase('test.db')


class BaseModel(Model):

    class Meta:
        database = mysql_db

    __metaclass__ = Meta

I have no idea how I can fix this, I'm new to Python. 我不知道如何解决这个问题,我是Python的新手。 My main goal is to get the program working with multiple classes. 我的主要目标是使程序与多个类一起使用。 That is why I'm trying to get flask classy to work. 这就是为什么我要让烧瓶优雅地工作。

A way to fix this problem without flask classy is just as welcome as any other fix. 与其他解决方案一样,没有烧瓶优雅的情况下解决此问题的方法也同样受欢迎。 If not using flask classy is more easy I'll give that a try. 如果不使用flask classy比较容易,我会尝试一下。

EDIT 编辑
When calling the metaclass bases, object.__init__() takes no parameters 调用元类基数时,object .__ init __()不带参数

class Meta(type):
    database = mysql_db

When I change the code to this I get the following error: 当我将代码更改为此时,出现以下错误:
TypeError: Error when calling the metaclass bases metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases TypeError:调用元类基础元类冲突时出错:派生类的元类必须是其所有基础元类的(非严格)子类

I managed to solve the problem by not using flask-classy. 我设法不使用flask-classy解决了这个问题。 Instead I'm using blueprints, flask documentation . 相反,我使用的是蓝图, flask文档 I no longer need both BaseModel and FlakView, only the BaseModel is needed now. 我不再需要BaseModel和FlakView,现在只需要BaseModel。

Here is my working code: 这是我的工作代码:
I no longer need FlaskView because I don't use flak-classy anymore. 我不再需要FlaskView,因为我不再使用flak-classy。 No more meteclass error! 没有更多的流星类错误!

_ init _.py _ 初始化 _.py

from flask import Flask
import user
app = Flask(__name__)
app.register_blueprint(user.bp)

user.py user.py

from base_model import BaseModel
class User(BaseModel):
    username = CharField(primary_key=True)
    password = CharField(null=False)

bp = Blueprint('user', __name__)

@bp.route('/user/method', method=['GET'])
def method()
#method stuff

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

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