无法从元类获得属性

时间:2016-06-27 03:33:44

标签: python django

运行此应用时出现以下错误:

AttributeError: type object 'ScientistObject' has no attribute 'fields'

这里的科学家对象

class ScientistObject(StandardValueObject):
  first_name  = fields.Primitive()
  last_name   = fields.Primitive()

这里是StandardValueObject

init
class StandardValueObject(object):
    __metaclass__ = ValueObjectMeta

    def __init__(self, filtered_data):
        super(StandardValueObject, self).__init__()

        self._fields = type(self).fields
        self._values = {
            name: field.init(filtered_data.get(field.key, None))
                for name, field in self._fields.iteritems()
        }

self._fields = type(self).fields是抛出错误的行。顺便说一句。

我还有一个fields.py,其中定义了类Field(对象)。

ValueObjectMeta是:

class ValueObjectMeta(type):

fields = None

    @staticmethod
    def __new__(mcs, name, bases, attrs):

        attrs[b'fields'] = {}

        for attr in [attr for attr, field in attrs.iteritems() if isinstance(field, Field)]:
            field = attrs.pop(attr)

            if not field.key:
                field.key = attr

            attrs[b'fields'][attr] = field

        return super(ValueObjectMeta, mcs).__new__(mcs, name, bases, attrs)

不应该让ScientistObject能够从元类中获取字段吗?

感谢您的帮助!

0 个答案:

没有答案