我已经为此工作了一段时间,我很难过。我一直在阅读文档...
https://www.django-rest-framework.org/api-guide/relations/#nested-relationships
并且仍然无法理解这应该如何工作。
如果有人可以帮助我解决这个错误或解释它应该如何工作,我会很高兴得到一些帮助。谢谢
我想要的是:
- 将ajax发送到django rest framework api端点
- 让 django / DRF 在 postgreSQL db 中保存多对多相关数据
我有什么:
TypeError: Session() 得到了一个意外的关键字参数“pose_list”
模型.py
class Pose(models.Model):
name = models.CharField(max_length=100, blank=False, default='')
def __str__(self):
return self.name
class SeshPoseList(models.Model):
order = models.PositiveIntegerField()
pose_fk = models.ForeignKey(Pose, on_delete=models.CASCADE)
class Session(models.Model):
session_name = models.CharField(max_length=100, blank=False, default='')
def __str__(self):
return self.name
序列化程序.py
class PoseSerializer(serializers.ModelSerializer):
class Meta:
model = Pose
fields = ('id', 'name')
class SeshPoseListSerializer(serializers.ModelSerializer):
def create(self, validated_data):
return Session.objects.create(**validated_data)
class Meta:
model = SeshPoseList
fields = ['order', 'pose_fk']
class SessionSerializer(serializers.ModelSerializer):
pose_list = SeshPoseListSerializer(many=True)
def create(self, validated_data):
return Session.objects.create(**validated_data)
class Meta:
model = Session
fields = ('id', 'session_name', 'pose_list')
视图.py
@api_view(['GET', 'POST', 'PUT', 'DELETE'])
def session_list(request):
if request.method == 'POST':
seshSerializer = SessionSerializer(data=request.data)
if seshSerializer.is_valid():
seshSerializer.create(seshSerializer.validated_data)
return Response(seshSerializer.data, status=status.HTTP_201_CREATED)
print("sesh errors ---> ", seshSerializer.errors)
return Response(seshSerializer.errors, status=status.HTTP_400_BAD_REQUEST)