本文整理匯總了Python中rest_framework.relations.PrimaryKeyRelatedField方法的典型用法代碼示例。如果您正苦於以下問題:Python relations.PrimaryKeyRelatedField方法的具體用法?Python relations.PrimaryKeyRelatedField怎麽用?Python relations.PrimaryKeyRelatedField使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類rest_framework.relations
的用法示例。
在下文中一共展示了relations.PrimaryKeyRelatedField方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: update_relationship_from_serializer
# 需要導入模塊: from rest_framework import relations [as 別名]
# 或者: from rest_framework.relations import PrimaryKeyRelatedField [as 別名]
def update_relationship_from_serializer(self, rv, field_instance, foreign_key_as_list):
if not isinstance(field_instance, (relations.PrimaryKeyRelatedField, relations.ManyRelatedField,
relations.SlugRelatedField)):
return
if not hasattr(field_instance, 'queryset') or field_instance.queryset is None:
return
related_model = field_instance.queryset.model
if not foreign_key_as_list:
self.update_related_endpoint(rv, related_model)
else:
if not hasattr(field_instance, 'queryset') or field_instance.queryset is None:
return
# FIXME: we may not need this code as the serializer field has a 'choices' attribute
self.set_choices_from_qs(rv, field_instance.queryset)
示例2: test_it_prefetches_foreign_key_when_source_is_changed
# 需要導入模塊: from rest_framework import relations [as 別名]
# 或者: from rest_framework.relations import PrimaryKeyRelatedField [as 別名]
def test_it_prefetches_foreign_key_when_source_is_changed(self):
car = ParentCar.objects.create()
parent = DeeplyNestedParent.objects.create(car=car)
class CarSerializer(ModelSerializer):
car_id = PrimaryKeyRelatedField(
source="car", queryset=ParentCar.objects.all()
)
class Meta:
model = DeeplyNestedParent
fields = ["car", "car_id"]
depth = 1
_run_test(CarSerializer, DeeplyNestedParent, 1)