- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在这段代码的第二行在 Django 中遇到上述错误:
survey = Survey.objects.get_or_create(organisation=organisation)
form = SurveyForm(instance=survey)
这是我的表格:
class SurveyForm(forms.ModelForm):
class Meta:
model = Survey
exclude = ['id']
widgets = {
'user' : forms.HiddenInput(),
'organisation' : forms.HiddenInput(),
'responsibleSecurityOfficer' : forms.TextInput(attrs={'class' : 'form-control'}),
'responsibleSecurityOfficerJobTitle' : forms.TextInput(attrs={'class' : 'form-control'}),
'size' : forms.Select(attrs={'class' : 'form-control'},
choices=SIZE_CHOICES),
'baseInfoSecurity' : forms.Select(attrs={'class' : 'form-control'}),
'pciCompliance' : forms.Select(attrs={'class' : 'form-control'}),
'hipaaCompliance' : forms.Select(attrs={'class' : 'form-control'}),
'internationalCompliance' : forms.Select(attrs={'class' : 'form-control'}),
'country' : forms.TextInput(attrs={'class' : 'form-control'}),
'drPlan' : forms.Select(attrs={'class' : 'form-control'}),
}
我真的不明白为什么它不起作用而且我没有看到任何错误的逗号(这似乎是基于类似帖子的普遍问题)。欢迎任何帮助。
编辑
回溯
File "/home/henry/Documents/Sites/Development/dashpliant/env/local/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/home/henry/Documents/Sites/Development/dashpliant/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/home/henry/Documents/Sites/Development/dashpliant/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/henry/Documents/Sites/Development/dashpliant/env/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/home/henry/Documents/Sites/Development/dashpliant/env/local/lib/python2.7/site-packages/django/contrib/auth/mixins.py" in dispatch
92. return super(PermissionRequiredMixin, self).dispatch(request, *args, **kwargs)
File "/home/henry/Documents/Sites/Development/dashpliant/env/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "/home/henry/Documents/Sites/Development/dashpliant/dashpliant/views.py" in get
38. form = SurveyForm(instance=survey)
File "/home/henry/Documents/Sites/Development/dashpliant/env/local/lib/python2.7/site-packages/django/forms/models.py" in __init__
297. object_data = model_to_dict(instance, opts.fields, opts.exclude)
File "/home/henry/Documents/Sites/Development/dashpliant/env/local/lib/python2.7/site-packages/django/forms/models.py" in model_to_dict
87. opts = instance._meta
Exception Type: AttributeError at /survey/
Exception Value: 'tuple' object has no attribute '_meta'
最佳答案
get_or_create
返回 (instance, created)
的元组,其中第二个元素是一个 bool 值,显示操作是否导致创建新项目。您应该单独捕获这些,而不是将整个内容传递给表单。您可以使用 _
表示您不关心 created
值。
survey, _ = Survey.objects.get_or_create(organisation=organisation)
form = SurveyForm(instance=survey)
关于python - AttributeError at/'tuple' 对象没有属性 '_meta',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46687451/
我正在尝试创建一个自定义对象管理器,如https://docs.djangoproject.com/en/dev/topics/db/managers/#modifying-initial-manag
我正在尝试将 django-voting 应用程序添加到我的项目中。我不知道如何在我的模板中使用它,所以我添加了一个新的模板标签,用于在用户单击按钮时投票赞成或反对。我不知道是否有一个很好的形式来做。
我克隆了 buildroot。在给出“make”命令后,它显示以下编译器错误。 /home/buildroot/instance-0/output/host/usr/bfin-buildroot-uc
我想问一下这段代码中._meta的使用?我没有找到解释 .meta 用法的文档 def resend_activation_email(self, request, queryset): ""
我正在尝试创建一个 View ,其中用户从下拉菜单中选择一个选项,提交它,然后返回一些数据。具体来说,他们将从数据库中的模型中进行选择,并返回该类的所有实例。我正在使用 django-tables2
我正在编写一个 shell 实用程序,它允许我遍历关系,这样我就不需要返回到我的模型并查看表是如何相关的。我正在使用 _meta为了获得所有这些信息。 查看以下示例模型: class Foo:
我在使用 Python 3.3 32 位的 Windows 上工作。我已经安装了 peewee 并想尝试它的一些功能。我已经开始使用 Peewee Quickstart ( http://peewee
我正在使用 python django 框架开发一个简单的应用程序,我正在使用基于类的 View ,当我使用 UpdateView 并尝试运行我的模板时,我得到了这个错误; 'QuerySet' ob
def participant_specific(request, participant): helper = RelayFunctions() info = helper.part
我有一个通用函数,它遍历给定对象的 _meta.fields。除 ManyToMany 字段外,所有字段名称和值均已正确获取。它似乎完全忽略了 ManyToMany 字段。我们如何从 m2m 字段中检
这是一个简单的模型定义,使用外键导入: from companies.models.owner_company import OwnerCompany class Restaurant(models.
我正在使用 cassandra 数据库和 django,我想使用 Django-rest-frame work 中的序列化程序序列化数据。 模型.py import uuid from cassand
我无法在 admin.py 中注册自定义用户模型。没有 admin.py ,一切正常 为什么要定制User?我不知道这是什么问题。我按照文档做了一切 模型.py from django.db impo
我有一个Location模型,其定义(大致)如下: from django.contrib.gis.db import models class Location(models.Model):
我正在尝试构建一个基本的条形图,但我在标题中遇到了错误。我已经使用 alert() 来验证我想要填充图表的数组是否包含数据,但是语法仍然没有正确地找出一些东西。有人可以检查并让我知道我需要做些什么才能
我正在 Django 项目中迁移我的模型,但正在生成错误。下面是代码。我的代码中应该进行哪些更正以解决此问题 Models.py from django.db import models from d
我在这段代码的第二行在 Django 中遇到上述错误: survey = Survey.objects.get_or_create(organisation=organisation)
当我尝试在模板中打印 usera._meta.get_all_field_names() 中的所有变量时,它给了我比预期多 2 个变量,我不知道为什么。 型号: class Clovek(models
如果我有类(class),要获得FK类(class),我可以这样做: MasterModel = Model._meta.get_field('master').rel.to 给定一个 MasterM
Django 抛出下一个异常: restframework 'tuple' 对象没有属性 '_meta' 模型 class BDetail(models.Model): lat = models
我是一名优秀的程序员,十分优秀!