我目前有一个包含以下字段的序列化程序class Meta: model = Asset fields = ('id', 'uuid', 'asset_category', 'asset_sub_category', 'make_label', 'asset_code', 'serial_number', 'model_number', 'checkin_status', 'created_at', 'last_modified', 'current_status', 'asset_type', 'allocation_history', 'specs', 'purchase_date', 'notes', 'assigned_to', 'asset_location' )序列化器class AssetSerializer(serializers.ModelSerializer): checkin_status = serializers.SerializerMethodField() allocation_history = serializers.SerializerMethodField() assigned_to = UserSerializer(read_only=True) asset_category = serializers.SerializerMethodField() asset_sub_category = serializers.SerializerMethodField() make_label = serializers.SerializerMethodField() asset_type = serializers.SerializerMethodField() model_number = serializers.SlugRelatedField( queryset=AssetModelNumber.objects.all(), slug_field="model_number" ) class Meta: model = Asset fields = ('id', 'uuid', 'asset_category', 'asset_sub_category', 'make_label', 'asset_code', 'serial_number', 'model_number', 'checkin_status', 'created_at', 'last_modified', 'current_status', 'asset_type', 'allocation_history', 'specs', 'purchase_date', 'notes', 'assigned_to', 'asset_location' ) depth = 1 read_only_fields = ("uuid",)看法class ManageAssetViewSet(ModelViewSet): serializer_class = AssetSerializer queryset = Asset.objects.all() # permission_classes = [IsAuthenticated, IsAdminUser] # authentication_classes = (FirebaseTokenAuthentication,) http_method_names = ['get', 'post', 'put', 'delete'] filter_backends = (filters.DjangoFilterBackend,) filterset_class = AssetFilter但是,在可浏览的 Api 上,UPDATE/PUT表单上只显示了 4 个字段,如下图所示其他一些字段没有出现在这里的原因可能是什么。什么决定了哪些字段是可更新的?
2 回答
有只小跳蛙
TA贡献1824条经验 获得超8个赞
好吧,问题是当您设置 depth = 1 ModelSerializer 时,会尝试为您未明确提及的任何外键相关字段生成 NestedSerializer 字段。而 NestedSerializer 字段是只读字段。这就是未显示资产位置的原因。删除该 depth = 1 行,DRF 将使用默认映射(即 PrimaryKeyRelatedFiel)映射所述字段,您将看到显示所述字段。
添加回答
举报
0/150
提交
取消