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)
评论列表
文章目录