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

Serializer as Field is not visible in json

型号

class makitVerifications (models.Model):

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'   , )

序列化器

class UserVerifyCountSerializer (serializers.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)

查看

class UserVerfiyCountViewSet(generics.ListCreateAPIView):

serializer_class = UserVerifyCountSerializer

def get_queryset(self):

    queryset = makitVerifications.objects.all()

    return queryset

JSON

{ "count": 2, "next":空, "previous":空, "results": [ { "user_fk": 1, "Matter_fk": 1 }, { "user_fk": 1, "Matter_fk": 1 } ]

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

我也尝试了文档中的示例,但 JSON

中缺少相同的字段

还有如何 omit/disable 来自对象的字段被嵌套在上面的序列化器中 class

Makie Serilazer

class makitSerializer(serializers.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)