bug之needs to have a value for field "id" before this many-to-many relationship can be used.

一、环境配置

  操作系统:WIndows 64

  django版本:3.0.3

  数据库:mysql

二、报错代码

c = Category()
c.name = cleaned_data.get("name")
c.brief = cleaned_data.get("brief")
c.author.add(User.objects.get(username=username))
c.save()
ValueError: "<Category: C++>" needs to have a value for field "id" before this many-to-many relationship can be used.

三、解决方法

c = Category()
c.name = cleaned_data.get("name")
c.brief = cleaned_data.get("brief")
c.save()
c.author.add(User.objects.get(username=username))
c.save()
原文地址:https://www.cnblogs.com/loveprogramme/p/12475352.html