简体   繁体   English

如何添加模型Django Restframework中未包含的字段

[英]How to add fields not in the model Django Restframework

I am trying to add a value to my response that is not in my models fields. 我试图为我的响应添加一个不在我的模型字段中的值。 For example, please take a look at the code below 例如,请看下面的代码

    for Me in Sections:
        Parts = Part.objects.filter(Section=Me.id)
        for Me in Parts: 
            FieldCount = Field.objects.filter(Req=True, Visible=True, Part=Me.id).count()
            Counter += FieldCount
    RequiredField = {'FieldsRequired': Counter}
    serializer = ApplicationSerializer(App)
    return Response(RequiredField)

I want to be able to send serializer.data in the Response and RequiredField in the Response. 我希望能够在Response和Response中的RequiredField中发送serializer.data。

I'm not quite sure whether I understand what you want but you can add additional values to your response by simply extending the data dictionary: 我不确定我是否了解您想要的内容,但是您可以通过简单地扩展数据字典来为响应添加其他值:

serializer = MyModelSerializer(object)
data = serializer.data
data['any_key'] = 'Any Value'
return Response(data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM