__init__.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:django-postgres-composite-types 作者: danni 项目源码 文件源码
def _capture_descriptors(cls):
        """Work around for not being able to call contribute_to_class.

        Too much code to fake in our meta objects etc to be able to call
        contribute_to_class directly, but we still want fields to be able
        to set custom type descriptors. So we fake a model instead, with the
        same fields as the composite type, and extract any custom descriptors
        on that.
        """

        attrs = {field_name: field for field_name, field in cls._meta.fields}

        # we need to build a unique app label and model name combination for
        # every composite type so django doesn't complain about model reloads
        class Meta:
            app_label = cls.__module__
        attrs['__module__'] = cls.__module__
        attrs['Meta'] = Meta
        model_name = '_Fake{}Model'.format(cls.__name__)

        fake_model = type(model_name, (models.Model,), attrs)
        for field_name, _ in cls._meta.fields:
            # default None is for django 1.9
            attr = getattr(fake_model, field_name, None)
            if inspect.isdatadescriptor(attr):
                setattr(cls, field_name, attr)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号