本文整理汇总了Python中rest_framework.compat.get_concrete_model函数的典型用法代码示例。如果您正苦于以下问题:Python get_concrete_model函数的具体用法?Python get_concrete_model怎么用?Python get_concrete_model使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_concrete_model函数的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: get_nested_field
def get_nested_field(self, model_field, related_model, to_many):
class NestedModelSerializer(self._model_serializer_class):
class Meta:
model = related_model
depth = self.opts.depth - 1
if not self.opts.nestedFields:
return NestedModelSerializer(many=to_many)
fieldName = None
if model_field:
fieldName = model_field.name
else: # else means it is a reverse relationship so the accessor_name must be retrieved
cls = self.opts.model
opts = get_concrete_model(cls)._meta
reverse_rels = opts.get_all_related_objects()
reverse_rels += opts.get_all_related_many_to_many_objects()
for relation in reverse_rels:
accessorName = relation.get_accessor_name()
if relation.model == related_model and accessorName in self.opts.nestedFields:
fieldName = accessorName
break
customFields = self.opts.nestedFields.get(fieldName)
if customFields is not None:
class CustomFieldSerializer(self._model_serializer_class):
class Meta:
model = related_model
fields = ['self'] + customFields[0] + list(customFields[1].keys())
nested_fields = customFields[1]
no_links = self.opts.noLinks
exclude = None
return CustomFieldSerializer(many=to_many)
return self.get_related_field(model_field, related_model, to_many)
开发者ID:seebass,项目名称:django-rest-hal,代码行数:35,代码来源:serializers.py
示例2: get_default_fields
def get_default_fields(self):
cls = self.opts.model
opts = get_concrete_model(cls)
fields = []
fields += [getattr(opts, field) for field in cls._fields_ordered]
ret = SortedDict()
for model_field in fields:
if isinstance(model_field, mongoengine.ObjectIdField):
field = self.get_pk_field(model_field)
else:
field = self.get_field(model_field)
if field:
field.initialize(parent=self, field_name=model_field.name)
ret[model_field.name] = field
for field_name in self.opts.read_only_fields:
assert field_name in ret,\
"read_only_fields on '%s' included invalid item '%s'" %\
(self.__class__.__name__, field_name)
ret[field_name].read_only = True
return ret
开发者ID:bollwyvl,项目名称:django-rest-framework-mongoengine,代码行数:25,代码来源:serializers.py
示例3: get_validation_exclusions
def get_validation_exclusions(self):
"""
Return a list of field names to exclude from model validation.
"""
cls = self.opts.model
opts = get_concrete_model(cls)._meta
exclusions = [field.name for field in opts.fields + opts.many_to_many]
for field_name, field in self.fields.items():
if field_name in exclusions and not field.read_only:
exclusions.remove(field_name)
return exclusions
开发者ID:dgaus,项目名称:django-rest-framework,代码行数:11,代码来源:serializers.py
示例4: get_validation_exclusions
def get_validation_exclusions(self, instance=None):
"""
Return a list of field names to exclude from model validation.
"""
cls = self.opts.model
opts = get_concrete_model(cls)._meta
exclusions = [field.name for field in opts.fields + opts.many_to_many]
for field_name, field in self.fields.items():
field_name = field.source or field_name
if field_name in exclusions \
and not field.read_only \
and (field.required or hasattr(instance, field_name)) \
and not isinstance(field, Serializer):
exclusions.remove(field_name)
return exclusions
开发者ID:the5fire,项目名称:django-rest-framework,代码行数:16,代码来源:serializers.py
示例5: __init__
def __init__(self, model, embedded_def_dict=None):
super(EmbeddedObjectsField, self).__init__()
self.model = model
self.embedded_def_dict = embedded_def_dict
self.opts = get_concrete_model(self.model)._meta
self.related_fields = [field for field in self.opts.fields if field.serialize if field.rel]
self.reverse_rels = self.opts.get_all_related_objects() + self.opts.get_all_related_many_to_many_objects()
self.related_fields_names = [field.name for field in self.related_fields]
self.reverse_rel_names = [relation.get_accessor_name() for relation in self.reverse_rels]
self.possible_embedded_names = set(
self.related_fields_names +
self.reverse_rel_names +
self.embedded_function_names() +
self.embedded_property_names())
开发者ID:mturilin,项目名称:easyapi,代码行数:16,代码来源:serializer.py
示例6: default_fields
def default_fields(self, nested=False):
"""
Return all the fields that should be serialized for the model.
"""
# TODO: Modfiy this so that it's called on init, and drop
# serialize/obj/data arguments.
#
# We *could* provide a hook for dynamic fields, but
# it'd be nice if the default was to generate fields statically
# at the point of __init__
cls = self.opts.model
opts = get_concrete_model(cls)._meta
pk_field = opts.pk
while pk_field.rel:
pk_field = pk_field.rel.to._meta.pk
fields = [pk_field]
fields += [field for field in opts.fields if field.serialize]
fields += [field for field in opts.many_to_many if field.serialize]
ret = SortedDict()
is_pk = True # First field in the list is the pk
for model_field in fields:
if is_pk:
field = self.get_pk_field(model_field)
is_pk = False
elif model_field.rel and nested:
field = self.get_nested_field(model_field)
elif model_field.rel:
to_many = isinstance(model_field,
models.fields.related.ManyToManyField)
field = self.get_related_field(model_field, to_many=to_many)
else:
field = self.get_field(model_field)
if field:
field.initialize(parent=self, field_name=model_field.name)
ret[model_field.name] = field
for field_name in self.opts.read_only_fields:
assert field_name in ret, \
"read_only_fields on '%s' included invalid item '%s'" % \
(self.__class__.__name__, field_name)
ret[field_name].read_only = True
return ret
开发者ID:izquierdo,项目名称:django-rest-framework,代码行数:47,代码来源:serializers.py
示例7: get_default_fields
def get_default_fields(self):
"""
Return all the fields that should be serialized for the model.
"""
cls = self.opts.model
assert cls is not None, \
"Serializer class '%s' is missing 'model' Meta option" % self.__class__.__name__
opts = get_concrete_model(cls)._meta
pk_field = opts.pk
# If model is a child via multitable inheritance, use parent's pk
while pk_field.rel and pk_field.rel.parent_link:
pk_field = pk_field.rel.to._meta.pk
fields = [pk_field]
fields += [field for field in opts.fields if field.serialize]
fields += [field for field in opts.many_to_many if field.serialize]
ret = SortedDict()
nested = bool(self.opts.depth)
is_pk = True # First field in the list is the pk
for model_field in fields:
if is_pk:
field = self.get_pk_field(model_field)
is_pk = False
elif model_field.rel and nested:
field = self.get_nested_field(model_field)
elif model_field.rel:
to_many = isinstance(model_field,
models.fields.related.ManyToManyField)
field = self.get_related_field(model_field, to_many=to_many)
else:
field = self.get_field(model_field)
if field:
ret[model_field.name] = field
for field_name in self.opts.read_only_fields:
assert field_name in ret, \
"read_only_fields on '%s' included invalid item '%s'" % \
(self.__class__.__name__, field_name)
ret[field_name].read_only = True
return ret
开发者ID:atlefren,项目名称:django-rest-framework,代码行数:46,代码来源:serializers.py
示例8: get_default_fields
def get_default_fields(self):
"""
Return all the fields that should be serialized for the model.
"""
cls = self.opts.model
opts = get_concrete_model(cls)._meta
pk_field = opts.pk
while pk_field.rel:
pk_field = pk_field.rel.to._meta.pk
fields = [pk_field]
fields += [field for field in opts.fields if field.serialize]
fields += [field for field in opts.many_to_many if field.serialize]
ret = SortedDict()
nested = bool(self.opts.depth)
is_pk = True # First field in the list is the pk
for model_field in fields:
if is_pk:
field = self.get_pk_field(model_field)
is_pk = False
elif model_field.rel and nested:
field = self.get_nested_field(model_field)
elif model_field.rel:
to_many = isinstance(model_field,
models.fields.related.ManyToManyField)
field = self.get_related_field(model_field, to_many=to_many)
else:
field = self.get_field(model_field)
if field:
field.initialize(parent=self, field_name=model_field.name)
ret[model_field.name] = field
for field_name in self.opts.read_only_fields:
assert field_name in ret, \
"read_only_fields on '%s' included invalid item '%s'" % \
(self.__class__.__name__, field_name)
ret[field_name].read_only = True
return ret
开发者ID:cmurtaugh,项目名称:django-rest-framework,代码行数:42,代码来源:serializers.py
示例9: restore_fields
def restore_fields(self, data, files):
"""
Core of deserialization, together with `restore_object`.
Converts a dictionary of data into a dictionary of deserialized fields.
"""
reverted_data = {}
copy_data = data.copy()
if data is not None and not isinstance(data, dict):
self._errors['non_field_errors'] = ['Invalid data']
return None
for field_name, field in self.fields.items():
copy_data.pop(field_name, None)
self.restore_initialize_field(field_name, field, data, files, reverted_data)
# what's left of the data
cls = self.opts.model
assert cls is not None, \
"Serializer class '%s' is missing 'model' Meta option" % self.__class__.__name__
opts = get_concrete_model(cls)._meta
nested = bool(self.opts.depth)
for field_name in copy_data.keys():
try:
model_field_tuple = opts.get_field_by_name(field_name)
model_field = model_field_tuple[0]
except models.FieldDoesNotExist:
continue
if model_field.rel:
related_model = _resolve_model(model_field.rel.to)
to_many = isinstance(model_field,
models.fields.related.ManyToManyField)
if nested:
field = self.get_nested_field(model_field, related_model, to_many)
else:
field = self.get_related_field(model_field, related_model, to_many)
self.restore_initialize_field(field_name, field, data, files, reverted_data)
return reverted_data
开发者ID:Proteus-tech,项目名称:drf_hal,代码行数:39,代码来源:serializers.py
示例10: get_default_fields
def get_default_fields(self):
"""
Overriding get_default_fields to do two things:
2. Add suffix '_id' to all foreign keys
"""
ret = super(AutoModelSerializer, self).get_default_fields()
# Deal with forward relationships
cls = self.opts.model
opts = get_concrete_model(cls)._meta
related_fields = [field for field in opts.fields if field.serialize if field.rel]
# adding embedded fields for the foreign keys
for model_field in related_fields:
field_name = model_field.name
del ret[field_name]
to_many = isinstance(model_field, ManyToManyField)
if not to_many:
related_model = _resolve_model(model_field.rel.to)
ret[field_name + '_id'] = self.get_related_field_with_source(model_field, related_model, to_many,
source=field_name)
# adding links field
ret['_meta'] = MetaField()
ret['_embedded'] = EmbeddedObjectsField(cls, embedded_def_dict=self.embedded_def_dict)
# adding fields for properties
for p_name, p_val in class_properties_with_attr(cls, 'field_class'):
field_name = getattr(p_val, 'name', p_name) or p_name # we use property name as field name by default
ret[field_name] = p_val.field_class(source=p_name)
return ret
开发者ID:mturilin,项目名称:easyapi,代码行数:38,代码来源:serializer.py
示例11: get_default_fields
def get_default_fields(self):
"""
Return all the fields that should be serialized for the model.
"""
cls = self.opts.model
assert cls is not None, \
"Serializer class '%s' is missing 'model' Meta option" % self.__class__.__name__
opts = get_concrete_model(cls)._meta
ret = SortedDict()
nested = bool(self.opts.depth)
# Deal with adding the primary key field
pk_field = opts.pk
while pk_field.rel and pk_field.rel.parent_link:
# If model is a child via multitable inheritance, use parent's pk
pk_field = pk_field.rel.to._meta.pk
field = self.get_pk_field(pk_field)
if field:
ret[pk_field.name] = field
# Deal with forward relationships
forward_rels = [field for field in opts.fields if field.serialize]
forward_rels += [field for field in opts.many_to_many if field.serialize]
for model_field in forward_rels:
if model_field.rel:
to_many = isinstance(model_field,
models.fields.related.ManyToManyField)
related_model = model_field.rel.to
if model_field.rel and nested:
if len(inspect.getargspec(self.get_nested_field).args) == 2:
warnings.warn(
'The `get_nested_field(model_field)` call signature '
'is due to be deprecated. '
'Use `get_nested_field(model_field, related_model, '
'to_many) instead',
PendingDeprecationWarning
)
field = self.get_nested_field(model_field)
else:
field = self.get_nested_field(model_field, related_model, to_many)
elif model_field.rel:
if len(inspect.getargspec(self.get_nested_field).args) == 3:
warnings.warn(
'The `get_related_field(model_field, to_many)` call '
'signature is due to be deprecated. '
'Use `get_related_field(model_field, related_model, '
'to_many) instead',
PendingDeprecationWarning
)
field = self.get_related_field(model_field, to_many=to_many)
else:
field = self.get_related_field(model_field, related_model, to_many)
else:
field = self.get_field(model_field)
if field:
ret[model_field.name] = field
# Deal with reverse relationships
if not self.opts.fields:
reverse_rels = []
else:
# Reverse relationships are only included if they are explicitly
# present in the `fields` option on the serializer
reverse_rels = opts.get_all_related_objects()
reverse_rels += opts.get_all_related_many_to_many_objects()
for relation in reverse_rels:
accessor_name = relation.get_accessor_name()
if not self.opts.fields or accessor_name not in self.opts.fields:
continue
related_model = relation.model
to_many = relation.field.rel.multiple
if nested:
field = self.get_nested_field(None, related_model, to_many)
else:
field = self.get_related_field(None, related_model, to_many)
if field:
ret[accessor_name] = field
# Add the `read_only` flag to any fields that have bee specified
# in the `read_only_fields` option
for field_name in self.opts.read_only_fields:
assert field_name not in self.base_fields.keys(), \
"field '%s' on serializer '%s' specfied in " \
"`read_only_fields`, but also added " \
"as an explict field. Remove it from `read_only_fields`." % \
(field_name, self.__class__.__name__)
assert field_name in ret, \
"Noexistant field '%s' specified in `read_only_fields` " \
"on serializer '%s'." % \
(self.__class__.__name__, field_name)
ret[field_name].read_only = True
#.........这里部分代码省略.........
开发者ID:andymckay,项目名称:django-rest-framework,代码行数:101,代码来源:serializers.py
示例12: get_default_fields
def get_default_fields(self):
"""
Return all the fields that should be serialized for the model.
"""
cls = self.opts.model
assert cls is not None, \
"Serializer class '%s' is missing 'model' Meta option" % self.__class__.__name__
opts = get_concrete_model(cls)._meta
ret = SortedDict()
nested = bool(self.opts.depth)
# Deal with adding the primary key field
pk_field = opts.pk
while pk_field.rel and pk_field.rel.parent_link:
# If model is a child via multitable inheritance, use parent's pk
pk_field = pk_field.rel.to._meta.pk
field = self.get_pk_field(pk_field)
if field:
ret[pk_field.name] = field
# Deal with forward relationships
forward_rels = [field for field in opts.fields if field.serialize]
forward_rels += [field for field in opts.many_to_many if field.serialize]
for model_field in forward_rels:
has_through_model = False
if model_field.rel:
to_many = isinstance(model_field,
models.fields.related.ManyToManyField)
related_model = _resolve_model(model_field.rel.to)
if to_many and not model_field.rel.through._meta.auto_created:
has_through_model = True
if model_field.rel and nested:
if len(inspect.getargspec(self.get_nested_field).args) == 2:
warnings.warn(
'The `get_nested_field(model_field)` call signature '
'is due to be deprecated. '
'Use `get_nested_field(model_field, related_model, '
'to_many) instead',
PendingDeprecationWarning
)
field = self.get_nested_field(model_field)
else:
field = self.get_nested_field(model_field, related_model, to_many)
elif model_field.rel:
if len(inspect.getargspec(self.get_nested_field).args) == 3:
warnings.warn(
'The `get_related_field(model_field, to_many)` call '
'signature is due to be deprecated. '
'Use `get_related_field(model_field, related_model, '
'to_many) instead',
PendingDeprecationWarning
)
field = self.get_related_field(model_field, to_many=to_many)
else:
field = self.get_related_field(model_field, related_model, to_many)
else:
field = self.get_field(model_field)
if field:
if has_through_model:
field.read_only = True
ret[model_field.name] = field
# Deal with reverse relationships
if not self.opts.fields:
reverse_rels = []
else:
# Reverse relationships are only included if they are explicitly
# present in the `fields` option on the serializer
reverse_rels = opts.get_all_related_objects()
reverse_rels += opts.get_all_related_many_to_many_objects()
for relation in reverse_rels:
accessor_name = relation.get_accessor_name()
if not self.opts.fields or accessor_name not in self.opts.fields:
continue
related_model = relation.model
to_many = relation.field.rel.multiple
has_through_model = False
is_m2m = isinstance(relation.field,
models.fields.related.ManyToManyField)
if (is_m2m and
hasattr(relation.field.rel, 'through') and
not relation.field.rel.through._meta.auto_created):
has_through_model = True
if nested:
field = self.get_nested_field(None, related_model, to_many)
else:
field = self.get_related_field(None, related_model, to_many)
if field:
#.........这里部分代码省略.........
开发者ID:matchimmo,项目名称:django-rest-framework,代码行数:101,代码来源:serializers.py
示例13: get_default_fields
def get_default_fields(self, base_fields):
"""
Return all the fields that should be serialized for the model.
"""
cls = self.opts.model
assert cls is not None, \
"Serializer class '%s' is missing 'model' Meta option" % self.__class__.__name__
opts = get_concrete_model(cls)._meta
ret = SortedDict()
nested = bool(self.opts.depth)
# Deal with adding the primary key field
pk_field = opts.pk
while pk_field.rel and pk_field.rel.parent_link:
# If model is a child via multitable inheritance, use parent's pk
pk_field = pk_field.rel.to._meta.pk
field = self.get_pk_field(pk_field)
if field:
ret[pk_field.name] = field
# Deal with forward relationships
forward_rels = [field for field in opts.fields if field.serialize]
forward_rels += [field for field in opts.many_to_many if field.serialize]
for model_field in forward_rels:
has_through_model = False
if model_field.rel:
to_many = isinstance(model_field,
models.fields.related.ManyToManyField)
related_model = _resolve_model(model_field.rel.to)
if to_many and not model_field.rel.through._meta.auto_created:
has_through_model = True
if model_field.rel and nested:
self.add_field_to_embedded(model_field.name, self.get_nested_field(model_field, related_model, to_many))
elif model_field.rel and model_field.name in base_fields:
key = model_field.name
field = base_fields[key]
if isinstance(field, HyperlinkedRelatedField):
self.add_field_to_links(key, field)
else:
self.add_field_to_embedded(key, field)
ret[model_field.name] = field
elif model_field.rel:
field = self.get_related_field(model_field, related_model, to_many)
if model_field.name not in self.additional_links:
self.add_field_to_links(model_field.name, field)
ret[model_field.name] = field
else:
if model_field.name in self.additional_links:
# already been added to links
continue
field = self.get_field(model_field)
if field:
if has_through_model:
field.read_only = True
ret[model_field.name] = field
# Deal with reverse relationships
reverse_rels = opts.get_all_related_objects()
reverse_rels += opts.get_all_related_many_to_many_objects()
for relation in reverse_rels:
accessor_name = relation.get_accessor_name()
if accessor_name not in self.opts.fields and accessor_name not in base_fields:
continue
related_model = relation.model
to_many = relation.field.rel.multiple
has_through_model = False
is_m2m = isinstance(relation.field,
models.fields.related.ManyToManyField)
if (is_m2m and
hasattr(relation.field.rel, 'through') and
not relation.field.rel.through._meta.auto_created):
has_through_model = True
if nested:
field = self.get_nested_field(None, related_model, to_many)
elif accessor_name in base_fields:
field = base_fields[accessor_name]
base_fields.pop(accessor_name)
else:
field = self.get_related_field(None, related_model, to_many)
if field:
self.add_field_to_embedded(accessor_name, field, has_through_model)
# Add the `read_only` flag to any fields that have bee specified
# in the `read_only_fields` option
for field_name in self.opts.read_only_fields:
assert field_name not in self.base_fields.keys(), (
"field '%s' on serializer '%s' specified in "
"`read_only_fields`, but also added "
"as an explicit field. Remove it from `read_only_fields`." %
#.........这里部分代码省略.........
开发者ID:Proteus-tech,项目名称:drf_hal,代码行数:101,代码来源:serializers.py
注:本文中的rest_framework.compat.get_concrete_model函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论