本文整理匯總了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)