Django tuple has no attribute get
I am trying to restrict the dropdown options according to the user type within the same form i.e usertype a has certain dropdown options and usertype b has certain dropdown options I tried to write my logic as below but am getting an error as the tuple has no attribute get
forms.py
class EditForm(forms.ModelForm):
class Meta:
model = Model
fields = ('title', 'info','status')
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super().__init__(args, **kwargs)
if self.user.is_usertypeA == True:
self.fields['status'].choices = (
('C','C'),
)
else:
self.fields['status'].choices = (
('A','A'),
('b','b'),
)
models.py
class EditView(LoginRequiredMixin,UpdateView):
template_name = 'edit.html'
model = model
form_class = EditForm
context_object_name = ''
success_url = reverse_lazy('')
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['user'] = self.request.user
return kwargs
def form_valid(self,form):
[![Error looks as this][1]][1]
error traceback
Internal Server Error: /app/dev/ticket/complete/39
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/handlers/base.py", line 204, in _get_response
response = response.render()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/template/response.py", line 105, in render
self.content = self.rendered_content
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/template/response.py", line 83, in rendered_content
return template.render(context, self._request)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/template/base.py", line 170, in render
return self._render(context)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/template/base.py", line 162, in _render
return self.nodelist.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/template/base.py", line 162, in _render
return self.nodelist.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/template/base.py", line 988, in render
output = self.filter_expression.resolve(context)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/template/base.py", line 671, in resolve
obj = self.var.resolve(context)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/template/base.py", line 796, in resolve
value = self._resolve_lookup(context)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/template/base.py", line 858, in _resolve_lookup
current = current()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/forms/forms.py", line 290, in as_p
return self._html_output(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/forms/forms.py", line 193, in _html_output
top_errors = self.non_field_errors().copy()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/forms/forms.py", line 304, in non_field_errors
return self.errors.get(NON_FIELD_ERRORS, self.error_class(error_class='nonfield'))
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/forms/forms.py", line 170, in errors
self.full_clean()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/forms/forms.py", line 372, in full_clean
self._clean_fields()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/forms/forms.py", line 384, in _clean_fields
value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/forms/widgets.py", line 263, in value_from_datadict
return data.get(name)
AttributeError: 'tuple' object has no attribute 'get'
[10/Dec/2021 12:02:00] "GET /app/dev/ticket/complete/39 HTTP/1.1" 500 179355
I think you need to override the entire field.
https://docs.djangoproject.com/en/2.2/topics/forms/modelforms/#overriding-the-default-fields
So set one of your options (the most common one) as the default on the form - add this code to your list of fields (i.e. above Meta
)
status = forms.Select(
widget=forms.Select(choices=CHOICES_OPTIONS_1),
)
Then override the widget if you need to by passing a new dict - you can replace your current if .. else
with this bit.
if not self.user.is_usertypeA:
widgets = {
'status': Select(choices=CHOICES_OPTIONS_2),
}
I've not tested this, but it should work.
Note the options 1 and 2 are meant to represent the tuples you give in the question - so the first one is C
and the second one would be A´ and
B` in your example.
Not entirely convinced __init__
is the place to do this either, but if it works ...