Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.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
Python 关于django模型库代码_Python_Django_Metaclass - Fatal编程技术网

Python 关于django模型库代码

Python 关于django模型库代码,python,django,metaclass,Python,Django,Metaclass,我正在阅读django的源代码,主要是关于模型的 in class ModelBase code ########################## def __new__(cls, name, bases, attrs): super_new = super(ModelBase, cls).__new__ # Also ensure initialization is only performed for subclasses of Model # (exclud

我正在阅读django的源代码,主要是关于模型的

in class ModelBase code
##########################

def __new__(cls, name, bases, attrs):
    super_new = super(ModelBase, cls).__new__

    # Also ensure initialization is only performed for subclasses of Model
    # (excluding Model class itself).
    parents = [b for b in bases if isinstance(b, ModelBase)]
    if not parents:
        return super_new(cls, name, bases, attrs)

    # Create the class.
    module = attrs.pop('__module__')
    new_class = super_new(cls, name, bases, {'__module__': module})
    attr_meta = attrs.pop('Meta', None)
    abstract = getattr(attr_meta, 'abstract', False)
    if not attr_meta:
        meta = getattr(new_class, 'Meta', None)
    else:
        meta = attr_meta
    base_meta = getattr(new_class, '_meta', None)
我很好奇:

    base_meta = getattr(new_class, '_meta', None)
因为新的_类是由

   new_class = super_new(cls, name, bases, {'__module__': module})
我认为新的_类将只有属性“module”,它在哪里可以得到“_meta”属性


因此,实际上我的问题是“在元类中。new,传入的属性是如何组合的?生成的类型实例的属性是如何组合的?”。我在谷歌上做了一个工作,我对调用序列有点了解,但这些细节仍然未知。

如果base中的某个类具有这些属性,那么
getattr
肯定会找到它们。这个新操作符是用来递归工作的。因此,当分析它时,你需要考虑这是否是第一个评价之一,或者是以后的评价中的任何一个。换句话说,
super\u new
很可能是相同的
\u new\u()
定义。