Django模型错误-"TypeError: 'xxx'是此函数的无效关键字参数

9

我收到了以下错误信息:

TypeError: 'person' is an invalid keyword argument for this function

我的模型是:

class Investment(models.Model):
company = models.ManyToManyField("Company", related_name ="Investments_company")
financial_org = models.ManyToManyField("Financial_org", related_name ="Investments_financial_org")
person = models.ManyToManyField("Person", related_name ="Investments_person")

我的测试(导致错误的测试):

investment1 = Investment(company = [], financial_org = financial1, person = [])
1个回答

26
  1. 实例化你的模型,不要使用多对多关系:investment1 = Investment()

  2. 保存你的模型:investment1.save()

  3. 添加多对多关系,有多种方法,比如:investment1.person.add(person_model)或者investment1.person.create(name='foo')

在模型保存前不能使用多对多关系,因为多对多关系表中的一行需要两端模型的主键。


你救了我的命。谢谢。 - Mr_Spock
这怎么能救命?不过感谢您的美言xD - jpic
朋友,谢谢你提供的解决方案,对我帮助很大。 - julian salas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接