ManyToMany django 字段不工作,不接受 POST 请求中的任何输入?
ManyToMany django field not working, not taking any input in POST request?
我有 2 个模型 - Module 和 Room。一个模块可以有零个或多个房间,一个房间可以添加到多个模块中。因此,它们之间存在简单的多对多关系。
但是当我在 module/models.py
文件中定义它时,它没有将任何输入作为 rooms
。这是我的文件 -
module/models.py -
class Module(models.Model):
module_id = models.AutoField(primary_key=True)
title = models.CharField(max_length=100)
desc = models.TextField()
rooms = models.ManyToManyField(Rooms)
rooms/models.py -
class Rooms(models.Model):
room_id = models.AutoField(primary_key=True)
title = models.CharField(max_length=100)
desc = models.TextField()
level = models.CharField(max_length=100)
is_deleted = models.BooleanField(default=False)
module/serializers.py -
class ModuleSerializer(serializers.ModelSerializer):
rooms = RoomSerializer(read_only=True, many=True)
class Meta:
model = Module
fields = "__all__"
module/views.py -
class add_module(APIView):
def post(self, request, format=None):
module_serializer = ModuleSerializer(data=request.data)
if module_serializer.is_valid():
module_serializer.save()
return Response(module_serializer.data['module_id'], status = status.HTTP_201_CREATED)
return Response(module_serializer.errors, status = status.HTTP_400_BAD_REQUEST)
POST 在 POSTMAN 中创建模块的请求正文 -
{
"rooms": [
{
"room_id": 2,
"title": "4",
"desc": "22",
"level": "2",
}
],
"title": "4",
"desc": "22",
}
根据此请求,正在创建模块,但没有向其中添加空间。
谁能告诉我为什么在创建模块时我的房间没有被添加?
您需要编写一个显式的序列化器创建方法来支持可写的嵌套表示。
https://www.django-rest-framework.org/api-guide/serializers/#writable-nested-representations
示例:
给定一个像这样的 RoomSerializer:
class RoomSerializer(serializers.ModelSerializer):
room_id = serializers.IntegerField()
class Meta:
model = models.Rooms
fields = '__all__'
我们在 RoomSerializer 中明确设置 room_id,以避免 从输入数据中剥离,并引发 MultipleObjectsReturned。
class ModuleSerializer(serializers.ModelSerializer):
rooms = RoomSerializer(many=True) # remove *read_only=True*
class Meta:
model = Module
fields = "__all__"
def create(self, validated_data):
rooms_data = validated_data.pop('rooms')
module = Module.objects.create(**validated_data)
for data in rooms_data:
room, created = Rooms.objects.get_or_create(**data)
module.rooms.add(room)
return module
我有 2 个模型 - Module 和 Room。一个模块可以有零个或多个房间,一个房间可以添加到多个模块中。因此,它们之间存在简单的多对多关系。
但是当我在 module/models.py
文件中定义它时,它没有将任何输入作为 rooms
。这是我的文件 -
module/models.py -
class Module(models.Model):
module_id = models.AutoField(primary_key=True)
title = models.CharField(max_length=100)
desc = models.TextField()
rooms = models.ManyToManyField(Rooms)
rooms/models.py -
class Rooms(models.Model):
room_id = models.AutoField(primary_key=True)
title = models.CharField(max_length=100)
desc = models.TextField()
level = models.CharField(max_length=100)
is_deleted = models.BooleanField(default=False)
module/serializers.py -
class ModuleSerializer(serializers.ModelSerializer):
rooms = RoomSerializer(read_only=True, many=True)
class Meta:
model = Module
fields = "__all__"
module/views.py -
class add_module(APIView):
def post(self, request, format=None):
module_serializer = ModuleSerializer(data=request.data)
if module_serializer.is_valid():
module_serializer.save()
return Response(module_serializer.data['module_id'], status = status.HTTP_201_CREATED)
return Response(module_serializer.errors, status = status.HTTP_400_BAD_REQUEST)
POST 在 POSTMAN 中创建模块的请求正文 -
{
"rooms": [
{
"room_id": 2,
"title": "4",
"desc": "22",
"level": "2",
}
],
"title": "4",
"desc": "22",
}
根据此请求,正在创建模块,但没有向其中添加空间。
谁能告诉我为什么在创建模块时我的房间没有被添加?
您需要编写一个显式的序列化器创建方法来支持可写的嵌套表示。
https://www.django-rest-framework.org/api-guide/serializers/#writable-nested-representations
示例:
给定一个像这样的 RoomSerializer:
class RoomSerializer(serializers.ModelSerializer):
room_id = serializers.IntegerField()
class Meta:
model = models.Rooms
fields = '__all__'
我们在 RoomSerializer 中明确设置 room_id,以避免
class ModuleSerializer(serializers.ModelSerializer):
rooms = RoomSerializer(many=True) # remove *read_only=True*
class Meta:
model = Module
fields = "__all__"
def create(self, validated_data):
rooms_data = validated_data.pop('rooms')
module = Module.objects.create(**validated_data)
for data in rooms_data:
room, created = Rooms.objects.get_or_create(**data)
module.rooms.add(room)
return module