如何 return manytomanyfield 作为 Django Rest Framework 中的对象 API
How to return manytomanyfield as object in Django RestFramework API
我尝试用 Django RestFramework 创建一个 API,所以我创建了 2 个模型 Note 和 Task,并在 Note 模型中有一个 ManyToManyField,这样我就可以在一个 Note 中放置很多任务,但是 API我创建的不是 return 完整的对象功能,而是 id。这是我的代码:
class NoteAPI(ListAPIView):
serializer_class = NoteSerializer
queryset = Note.objects.all()
这是我的模型:
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"
我希望输出罐看起来像:
[
{
"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
}
]
}
]
但实际上是这样
[
{
"id": 2,
"title": "Sleep",
"priority": "F",
"detail": "Don't do it, stay awake and do your job",
"completed": false,
"process": 0,
"task": [
1,
2
]
}
]
那么我该怎么做才能得到这样的输出呢?
你需要告诉NoteSerializer
如何序列化Task
objectcs。
class TaskSerializer(serializers.ModelSerializer):
class Meta:
model = Task
fields = ("id", "task", "completed")
class NoteSerializer(models.Model):
task = TaskSerializer(many=True)
您可能还想预取任务以将数据库查询的数量保持在最低限度。
class NoteAPI(ListAPIView):
serializer_class = NoteSerializer
queryset = Note.objects.prefetch_related("task").all()
我尝试用 Django RestFramework 创建一个 API,所以我创建了 2 个模型 Note 和 Task,并在 Note 模型中有一个 ManyToManyField,这样我就可以在一个 Note 中放置很多任务,但是 API我创建的不是 return 完整的对象功能,而是 id。这是我的代码:
class NoteAPI(ListAPIView):
serializer_class = NoteSerializer
queryset = Note.objects.all()
这是我的模型:
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"
我希望输出罐看起来像:
[
{
"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
}
]
}
]
但实际上是这样
[
{
"id": 2,
"title": "Sleep",
"priority": "F",
"detail": "Don't do it, stay awake and do your job",
"completed": false,
"process": 0,
"task": [
1,
2
]
}
]
那么我该怎么做才能得到这样的输出呢?
你需要告诉NoteSerializer
如何序列化Task
objectcs。
class TaskSerializer(serializers.ModelSerializer):
class Meta:
model = Task
fields = ("id", "task", "completed")
class NoteSerializer(models.Model):
task = TaskSerializer(many=True)
您可能还想预取任务以将数据库查询的数量保持在最低限度。
class NoteAPI(ListAPIView):
serializer_class = NoteSerializer
queryset = Note.objects.prefetch_related("task").all()