Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError at / 'tuple' object has no attribute '_meta'

I'm getting the above error in Django on the second line this code:

    survey = Survey.objects.get_or_create(organisation=organisation)
    form = SurveyForm(instance=survey)

This is my form:

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

I really don't understand why it is not working and I do not see any erroneous commas (which seems to be the general issue based on similar posts). Any help most welcome.

EDIT

Traceback

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'
like image 498
HenryM Avatar asked Mar 15 '26 16:03

HenryM


1 Answers

get_or_create returns a tuple of (instance, created) where the second element is a boolean showing whether or not the operation resulted in a new item being created. You should capture these separately, rather than passing the whole thing to the form. You can use _ to indicate that you don't care about the created value.

survey, _ = Survey.objects.get_or_create(organisation=organisation)
form = SurveyForm(instance=survey)
like image 95
Daniel Roseman Avatar answered Mar 18 '26 06:03

Daniel Roseman