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

标签 python django django-models

我得到错误:

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. 在没有多对多的情况下实例化您的模型investment1 = Investment()

  2. 保存您的模型investment1.save()

  3. 多对多相加,有several ways to do itinvestment1.person.add(person_model)investment1.person.create(name='foo')

在保存模型之前,您不能使用 ManyToMany 关系,这是因为 ManyToMany 关系表中的一行需要关系两侧模型的 pk。

关于python - Django 模型错误 - "TypeError: ' xxx' 是此函数的无效关键字参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8677750/

相关文章:

python - 为什么 FAST/ORB 不能在图像边缘附近找到关键点

python - CPython 还是 IronPython?

python - 如何使用 Python 中的 Rest API 进行身份验证并从 Azure 数据目录获取目录

python - 将QMessageBox系统图标添加到QDialog中

python - django 方法是存在的,但是当我尝试调用它时评估为 NoneType

python - Django:将行插入数据库

json - 我应该如何从 bradjasper 的 django-jsonfield 升级到 Django 的内置 jsonfield?

python - Djangocms 中的自定义 View ?

Django - ChoiceField - 选项按钮而不是选择框

django - 通过运行 sqlall 在 Django 中生成 SQL 命令