当前位置: 首页>>代码示例>>Python>>正文


Python relations.PrimaryKeyRelatedField方法代码示例

本文整理汇总了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) 
开发者ID:drf-forms,项目名称:drf-schema-adapter,代码行数:19,代码来源:get_field_dict.py

示例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) 
开发者ID:GeeWee,项目名称:django-auto-prefetching,代码行数:17,代码来源:tests.py


注:本文中的rest_framework.relations.PrimaryKeyRelatedField方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。