Alex
Alex

Reputation: 540

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.

Upvotes: 1

Views: 160

Answers (1)

Lukas Bünger
Lukas Bünger

Reputation: 4487

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)

Upvotes: 2

Related Questions