本文整理汇总了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)