2017-10-11 104 views
0

我得到上述錯誤在Django第二行的代碼:AttributeError的AT/'元組' 對象有沒有屬性 '_meta'

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'}), 
     } 

我真的不明白爲什麼它不起作用,我沒有看到任何錯誤的逗號(這似乎是基於類似帖子的一般問題)。任何幫助最受歡迎。

EDIT

回溯

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' 
+0

顯示錯誤的追溯。 –

+1

'get_or_create()'返回一個帶有對象和一個標誌的元組,如果它是新創建的,那麼試試'survey,created = Survey.objects.get_or_create(...)' –

+0

謝謝克勞斯 - 我知道這一點。我只是看不到它 – HenryM

回答

2

get_or_create返回(instance, created)的元組,其中所述第二元件是示出操作是否導致正在創建新項的布爾值。你應該分開捕捉這些,而不是將整個事情傳遞給表單。您可以使用_來表示您不關心created值。

survey, _ = Survey.objects.get_or_create(organisation=organisation) 
form = SurveyForm(instance=survey) 
相關問題