Home > Software design >  How to return manytomanyfield as object in Django RestFramework API
How to return manytomanyfield as object in Django RestFramework API

Time:12-19

I tried to create an API with Django RestFramework, so i created 2 models Note and Task and have a ManyToManyField in Note model so i can put many Task in a Note but the API i created don't return full object feature but just the id. Here is my code:

class NoteAPI(ListAPIView):
    serializer_class = NoteSerializer
    queryset = Note.objects.all()

Here is my models:

class Task(models.Model):
    task = models.CharField(max_length=255, null=False, blank=False)
    detail = models.CharField(max_length=255, null=True, blank=True)
    completed = models.BooleanField(default=False)
    priority = models.IntegerField(default=0)
    def __str__(self):
        return self.task

class Note(models.Model):
    title = models.CharField(max_length=255, null=False, blank=False)
    priority = models.CharField(max_length=1, choices=PRIORITY_CHOICES, default="B")
    detail = models.CharField(max_length=255, null=True, blank=True)
    completed = models.BooleanField(default=False)
    task = models.ManyToManyField(Task, related_name="note_task", blank=True)
    process = models.IntegerField( default=0, validators=[max_int_value])
    def __str__(self) -> str:
        return self.title   " is "  str(self.process)   "% completed"

And i want the out put can looks like:

 [
    {
            "id": 2,
            "title": "Sleep",
            "priority": "F",
            "detail": "Don't do it, stay awake and do your job",
            "completed": false,
            "process": 0,
            "task": [
                      {
                         "id": 1,
                         "task": "Go to bed",
                         "completed": false
                      },
                     {
                         "id": 2,
                         "task": "Start counting",
                         "completed": false
                      }
                    ]
     }
 ]

But it actually be like this

 [
    {
            "id": 2,
            "title": "Sleep",
            "priority": "F",
            "detail": "Don't do it, stay awake and do your job",
            "completed": false,
            "process": 0,
            "task": [
                      1,
                      2
                 ]
     }
 ]

So what i can do to have the out put like that?

CodePudding user response:

You need to tell NoteSerializer how to serialize Task objectcs.

class TaskSerializer(serializers.ModelSerializer):
    class Meta:
        model = Task
        fields = ("id", "task", "completed")


class NoteSerializer(models.Model):
    task = TaskSerializer(many=True)

You probably also want to prefetch tasks to keep the number of DB queries to minimum.

class NoteAPI(ListAPIView):
    serializer_class = NoteSerializer
    queryset = Note.objects.prefetch_related("task").all()
  • Related