0

I've created a REST API that populate data from Model_A. but now I need to add another data field into the API that coming from another model. How can I add the parent field from the ModelParent into my API and the parent field also has to be associated properly with Model_A project for example below is an example I'm trying to do but not working (NOTE: model.py are not allowed to be altered in anyway) :

API :

class ReportAPI(APIView):
    def get(self, request):
        project_data = []
        all_projects = Model_A.objects.all()
        for project in all_projects:
            project_data.append(project)
            project_parent = ModelParent.objects.filter(project=project.id)
            for parent in project_parent:
                project_data.append(parent)
        project_serializer = SerializerA(all_projects, many=True)
        return Response(project_serializer.data)

Rest of the required code :

Model :

class Model_A(models.Model):
    project_name = models.CharField(max_length=50, unique=True)
    project_type = models.CharField(max_length=50, choices=p_type)

class ModelParent(models.Model):
    project = models.ForeignKey('Model_A', on_delete=models.CASCADE, related_name='+')
    parent = models.ForeignKey('ModelParent', on_delete=models.SET(''), related_name='+', null=True, blank=True)

Serializer :

class SerializerA(serializers.ModelSerializer):
    class Meta:
        model = Model_A
        fields = ('id', 'project_name','project_type')
        depth = 1

Current output of my API

[
    {
        "id": 1,
        "project_name": "Project A",
        "project_type": "advertisement",
    }
]

Desired Output :

[
    {
        "id": 1,
        "project_name": "Project A",
        "project_type": "advertisement",
        "parent_list": [
                        {
                         id : "1",
                         parent : "Parent 1",
                        },
                        {
                         id : "2",
                         parent : "Parent 2",
                        },
                       ]
    }
]

Any help is much appreciated thanks!

1 Answer 1

1

The Corresponding Model would be as follows: :

class ModelParent(models.Model):
    project = models.ForeignKey('Model_A', on_delete=models.CASCADE, related_name='project')
    parent = models.ForeignKey('ProjectContent', on_delete=models.SET(''), related_name='project_parent', null=True, blank=True)

Serializer would be as follows:

class ModelParentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Model_A
        fields = ('id', 'parent')


class SerializerA(serializers.ModelSerializer):
    parent_list = serializers.SerializerMethodField()

    class Meta:
        model = Model_A
        fields = ('id', 'project_name','project_type', 'parent_list')
        depth = 1

    def get_parent_list(self, obj):
        return ModelParentSerializer(obj.project, many=True).data #project is related name of project field
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @Maulik thanks thanks for your input, my apologies i should've mention that I can't tempered with the model in anyway at all. nevertheless i''ll still try to implement your code tqvm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.