Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Additional Data to a Serialize Response in Django

Updated

I changed my simplified question into a real example.

I've created a working post response of data from the model using ModelSerialzer, which I call from a post method in a view class. I would like to add additional data to the response. This is the pertinent code from my CBV:

def post(self, request, format=None):
    user_profile = UserProfiles.objects.get(user=request.user.id)
    service_id = user_profile.service_id
    rec_filter = Recommendations.objects.values_list('resource')
    if service_id > 0:
        service_name = Services.objects.get(pk=service_id)
        programs = Programs.objects.filter(services=service_id)
        resources_filtered = Resources.objects.filter(program__in=programs).exclude(id__in=rec_filter)
    else:
        service_name = 'All Services'
        resources_filtered = Resources.objects.exclude(id__in=rec_filter)

    serializer = ResourceSerializer(resources_filtered, many=True)
    #serializer.data["service"] = service_name
    return Response(serializer.data)

The commented out line was my attempt to add data base on a similar post here. I get a 500 response in my API call. What is the correct way to do it? The response data is JSON if that's necessary to mention.

This is the ModelSerializer:

class ResourceSerializer(serializers.ModelSerializer):
organization = OrganizationSerializer(read_only=True)
program = ProgramSerializer(read_only=True)

class Meta:
    model = Resources
    fields = [
        'organization',
        'program',
        'link',
        'contact',
        'general_contact',
        'eligibility',
        'service_detail'
    ]

Test of the answer

Heres the updated code based on the answer with a correction to fix and error:

class ResourceSerializer(serializers.ModelSerializer):
organization = OrganizationSerializer(read_only=True)
program = ProgramSerializer(read_only=True)
service = serializers.SerializerMethodField()

def get_service(self, obj):
    return "All Services"

class Meta:
    model = Resources
    fields = [
        'organization',
        'program',
        'link',
        'contact',
        'general_contact',
        'eligibility',
        'service_detail',
        'service'
    ]

The problem with this approach is that the value "All Services" is repeated in every row serialized. It's only needed once. I'd also like to keep the data transmitted minimized.

like image 828
curt Avatar asked Apr 17 '26 16:04

curt


1 Answers

You can do it in serializer itself. Define the new field required and add it in fields. Mark all the fields in serializer from resource model.

class ResourceSerializer(serializers.ModelSerializer):
    service = serializers.SerializerMethodField()

    def get_service(self):
            return "All Services"

    class Meta :
        model = Resources
        fields = ('service')           #Mark all the fields required here from resource model
like image 115
Shivendra Pratap Kushwaha Avatar answered Apr 20 '26 08:04

Shivendra Pratap Kushwaha