本文整理汇总了Python中rest_framework.serializers.HyperlinkedIdentityField方法的典型用法代码示例。如果您正苦于以下问题:Python serializers.HyperlinkedIdentityField方法的具体用法?Python serializers.HyperlinkedIdentityField怎么用?Python serializers.HyperlinkedIdentityField使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rest_framework.serializers
的用法示例。
在下文中一共展示了serializers.HyperlinkedIdentityField方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_related_object_url
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import HyperlinkedIdentityField [as 别名]
def get_related_object_url(self, obj, field):
try:
obj = getattr(obj, field)
if callable(obj):
obj = obj()
default_view_name = '%(model_name)s-detail'
format_kwargs = {
'app_label': obj._meta.app_label,
'model_name': obj._meta.object_name.lower()
}
view_name = default_view_name % format_kwargs
s = serializers.HyperlinkedIdentityField(source=obj, view_name=view_name)
s.initialize(self, None)
return s.field_to_native(obj, None)
except Exception as e:
print('WARN', e)
return None
示例2: to_representation
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import HyperlinkedIdentityField [as 别名]
def to_representation(self, value):
# HyperlinkedIdentityField demands a request when creating
# urls, but one is not always available. Instead of erroring
# out hard, fake it.
if "request" not in self.context:
self.context["request"] = None
return super().to_representation(value)
示例3: get_resources
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import HyperlinkedIdentityField [as 别名]
def get_resources(self, obj):
child = serializers.HyperlinkedIdentityField(
view_name=self.resources_view_name
)
field = serializers.ListField(child=child)
field.parent = self
return field.to_representation(obj.resources.all())
示例4: _get_related_fields
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import HyperlinkedIdentityField [as 别名]
def _get_related_fields(self, fields, id_field):
return [k for (k, v) in fields
if k != id_field
and (isinstance(v, HyperlinkedRelatedField)
or isinstance(v, HyperlinkedIdentityField)
or isinstance(v, ItemLinkField)
or (isinstance(v, ManyRelatedField)
and isinstance(v.child_relation, HyperlinkedRelatedField)))]
示例5: __init__
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import HyperlinkedIdentityField [as 别名]
def __init__(self, kind=None, **kwargs):
self.view_name = "archives-distillation"
self.distillation = kind
serializers.HyperlinkedIdentityField.__init__(
self, view_name=self.view_name, **kwargs)