简体   繁体   中英

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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