gpt4 book ai didi

django - 作为字段的序列化程序在 json 中不可见

转载 作者:行者123 更新时间:2023-12-01 08:27:07 25 4
gpt4 key购买 nike

型号

类 makitVerifications(模型.模型):

Matter_fk = models.ForeignKey(Matter,on_delete=models.CASCADE , related_name = "makit_validate")

user_fk = models.ForeignKey('auth.User',on_delete=models.CASCADE , related_name = 'verification_by' , )

序列化器

类 UserVerifyCountSerializer(序列化器.ModelSerializer):

# Tried makit_validate = makitSerializer (read_only = True)

class Meta :

model = makitVerifications
fields = ( 'user_fk', 'makit_validate', 'Matter_fk')

UserVerifyCountSerializer._declared_fields['makit_validate']= makitSerializer (read_only = True)

查看

类 UserVerfiyCountViewSet(generics.ListCreateAPIView):

serializer_class = UserVerifyCountSerializer

def get_queryset(self):

queryset = makitVerifications.objects.all()

return queryset

JSON

{ “计数”:2, “下一个”:空, “上一个”:空, “结果”: [ { “user_fk”:1, “Matter_fk”:1 }, { “user_fk”:1, “Matter_fk”:1 } ]

JSON 中缺少预期作为嵌套对象的字段“makit_validate”。如何在 json 中获取相关模型的嵌套对象。

我也尝试了文档中的示例,但 JSON 中缺少相同的字段

还有如何省略/禁用嵌套在上述序列化程序类中的对象的字段

Makie Serilazer

类 makitSerializer(序列化器.ModelSerializer):

kamaz = kamazSerializer(many=True,read_only=True)

facilities = FacilitiesSerializer (many =True,read_only=True)

class Meta :

model = Makit

fields = '__all__'

## Makit Model ##
class Makit (models.Model):
pincode = models.IntegerField()

area = models.CharField(max_length=200)

street = models.CharField(max_length=200)

landmark = models.CharField(max_length=100,blank=True , null = True)

contact_no= models.BigIntegerField()

最佳答案

来自此处的文档:Serializer fields

尝试:

makit_validate = makitSerializer(source="the_field_you_find_makit" read_only = True)

关于django - 作为字段的序列化程序在 json 中不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39059827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com