Django 嵌套序列化程序 - Return 如果满足条件则为空字段
Django Nested Serializer - Return Null Field if condition is fullfiled
我有一个嵌套的序列化,如果父序列化器字段“is_profile_private”(布尔值)为真,我需要将其内容 return 设为 Null。
我尝试使用 get_queryset 来过滤用户配置文件,但没有取得任何进展。
尝试使用 SerializerMethordField() 和 get_profile() 但 Django 抱怨 UserProfileSerializer 类型的对象不允许被序列化。
serializers.py
class UserProfileSerializer(UserSerializer):
height = serializers.SerializerMethodField()
class Meta:
model = UserProfile
fields = (
"bio",
"gender",
"custom_gender",
"non_binary_list",
"birthdate",
"avatar",
"height",
"hometown",
"zodiac_sign",
"language",
)
@staticmethod
def get_height(obj):
return {"value": obj.height, "unit": obj.height_unit}
class SimpleUserSerializer(UserSerializer):
profile = UserProfileSerializer(source="user", required=False)
class Meta:
model = User
fields = (
"id",
"name",
"username",
"is_profile_private",
"date_joined",
"profile",
)
views.py
class UserProfileAPIView(RetrieveModelMixin, UpdateModelMixin, GenericViewSet):
lookup_field = "id"
queryset = User.objects.all()
serializer_class = SimpleUserSerializer
http_method_names = ["get"]
@staticmethod
def get(request, *args, **kwargs):
return User.objects.get(id=str(request.data))
您可以使用 SerializerMethodField
:
class SimpleUserSerializer(UserSerializer):
profile = serializers.SerializerMethodField()
class Meta:
model = User
fields = (
"id",
"name",
"username",
"is_profile_private",
"date_joined",
"profile",
)
def get_profile(self, obj):
if obj.is_profile_private:
return None
return UserProfileSerializer(obj.user).data
请注意,您应该return序列化程序的数据,而不是序列化程序本身。
我有一个嵌套的序列化,如果父序列化器字段“is_profile_private”(布尔值)为真,我需要将其内容 return 设为 Null。 我尝试使用 get_queryset 来过滤用户配置文件,但没有取得任何进展。 尝试使用 SerializerMethordField() 和 get_profile() 但 Django 抱怨 UserProfileSerializer 类型的对象不允许被序列化。
serializers.py
class UserProfileSerializer(UserSerializer):
height = serializers.SerializerMethodField()
class Meta:
model = UserProfile
fields = (
"bio",
"gender",
"custom_gender",
"non_binary_list",
"birthdate",
"avatar",
"height",
"hometown",
"zodiac_sign",
"language",
)
@staticmethod
def get_height(obj):
return {"value": obj.height, "unit": obj.height_unit}
class SimpleUserSerializer(UserSerializer):
profile = UserProfileSerializer(source="user", required=False)
class Meta:
model = User
fields = (
"id",
"name",
"username",
"is_profile_private",
"date_joined",
"profile",
)
views.py
class UserProfileAPIView(RetrieveModelMixin, UpdateModelMixin, GenericViewSet):
lookup_field = "id"
queryset = User.objects.all()
serializer_class = SimpleUserSerializer
http_method_names = ["get"]
@staticmethod
def get(request, *args, **kwargs):
return User.objects.get(id=str(request.data))
您可以使用 SerializerMethodField
:
class SimpleUserSerializer(UserSerializer):
profile = serializers.SerializerMethodField()
class Meta:
model = User
fields = (
"id",
"name",
"username",
"is_profile_private",
"date_joined",
"profile",
)
def get_profile(self, obj):
if obj.is_profile_private:
return None
return UserProfileSerializer(obj.user).data
请注意,您应该return序列化程序的数据,而不是序列化程序本身。