【发布时间】:2018-09-23 16:13:40
【问题描述】:
我有两个模型:
class Someinfo(models.Model):
name = models.CharField(max_length=200)
#something else
class OtherInfo(models.Model):
name2 = models.CharField(max_lenth=200)
related_someinfo = models.ManyToManyField(Someinfo)
#something else
现在我已经创建了 CBV 视图来创建和查看它们。 CreateView 工作正常并保存可以在管理员中查看的信息,但我无法让模板在任何其他视图上显示数据,无论是 FormView、DetailView 还是任何其他视图,因为我收到此错误:
__call__() missing 1 required keyword-only argument: 'manager'
Request Method: GET
Request URL: http://something
Django Version: 2.0.3
Exception Type: TypeError
Exception Value:
__call__() missing 1 required keyword-only argument: 'manager'
Exception Location: /usr/local/lib/python3.5/dist-packages/django/forms/forms.py in get_initial_for_field, line 494
Python Executable: /usr/bin/python3
Python Version: 3.5.3
检查forms.py中的行它表明不起作用的功能是:
def get_initial_for_field(self, field, field_name):
"""
Return initial data for field on form. Use initial data from the form
or the field, in that order. Evaluate callable values.
"""
value = self.initial.get(field_name, field.initial)
if callable(value):
value = value() # line 494
return value
有什么建议吗?我可以通过shell查询链接的对象,它们保存在数据库中,所以我不知道如何继续。
【问题讨论】:
-
您不需要显示来自 Django 的
forms.py- 该错误可能是由您的代码引起的,您没有显示。请添加失败的视图。听起来您的表单和模型名称可能会相互冲突,所以如果您使用像Someinfo这样的虚构名称,您可能会隐藏问题。 -
谢谢你,Alasdair,你救了我。我的错误确实在views.py中。我是通过 m2m 链接直接调用初始数据,而不是使用 .all()。
标签: python-3.x django