當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。