gpt4 book ai didi

python - AttributeError at/'tuple' 对象没有属性 '_meta'

转载 作者:太空宇宙 更新时间:2023-11-04 09:53:56 25 4
gpt4 key购买 nike

我在这段代码的第二行在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com