具有相同模型的Django多个ManyToManyField

问题描述

当我只有一个 ManyToManyField 时,模型可以工作,但是当我添加第二个时,序列化程序不会保存。

查看:

@api_view(['POST'])
def update(request,pk):
    poll = Poll.objects.get(id=pk)
    serializer = PollSerializer(instance=poll,data=request.data)
    if serializer.is_valid():
        serializer.save()
    return Response(serializer.data)

型号:

class Poll(models.Model):
    title = models.CharField(max_length=200)
    option1 = models.CharField(max_length=200)
    option2 = models.CharField(max_length=200)
    option1total = models.IntegerField(default=0)
    option2total = models.IntegerField(default=0)
    owner = models.CharField(max_length=150,null=True)
    option1Votes = models.ManyToManyField(User,related_name="option1Votes")
    option2Votes = models.ManyToManyField(User,related_name="option2Votes")

示例请求:

{'id': 17,'title': 'What is your favorite programming language?','option1': 'Javascript','option2': 'Python','option1total': 2,'option2total': 25,'owner': None,'option1Votes': [],'option2Votes': [14]}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)