I would alter user input in the clean methods.
https://docs.djangoproject.com/en/2.0/ref/forms/validation/
From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On Behalf Of clavierplayer@gmail.com
Sent: Monday, July 16, 2018 10:28 AM
To: Django users
Subject: help with get_form_kwargs
I have a CreateView in which I need to alter the user input. I may have found a way to do so using get_form_kwargs, but I'm puzzled by a couple of things:
I know that kwargs['data'] is a QueryDict, which is immutable. So I tried the doc's QueryDict.copy() recommendation:
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
clone = kwargs['data'].copy()
# clone = copy(kwargs['data'])
item_num = clone['item']
actual_item = Item.objects.get(item_number=item_num)
clone['item'] = str(actual_item.id)
# data.update({'item': actual_item})
kwargs['data'] = clone
return kwargs
It appears to work just fine until the return statement. I can step through every line and see the values change exactly the way I want them to change. By the time I get to the return statement, `clone`--which is a copy of kwargs['data']--is a QueryDict that contains the correct information. However, the program crashes with a KeyError after `kwargs` is returned, saying that kwargs['data'] doesn't exist. In the following stack trace, line 62 is the second line of the method (clone = kwargs['data'].copy()).
KeyError at /utils/box-optimization/add-items/115
'data' | Request Method: | GET |
| Request URL: | |
| Django Version: | 2.0.5 |
| Exception Type: | KeyError |
| Exception Value: | 'data' |
| Exception Location: | C:\WMS Repository\Warehouse Management System\utils\views.py in get_form_kwargs, line 62 |
| Python Executable: | C:\miniconda3\envs\django\python.exe |
| Python Version: | 3.6.5 |
| Python Path: | ['C:\\WMS Repository\\Warehouse Management System', 'C:\\Program Files\\JetBrains\\PyCharm 2017.3.3\\helpers\\pydev', 'C:\\WMS Repository\\Warehouse Management System', 'C:\\Program Files\\JetBrains\\PyCharm 2017.3.3\\helpers\\pydev', 'C:\\Users\\heast\\.PyCharm2018.1\\system\\cythonExtensions', 'C:\\miniconda3\\envs\\django\\python36.zip', 'C:\\miniconda3\\envs\\django\\DLLs', 'C:\\miniconda3\\envs\\django\\lib', 'C:\\miniconda3\\envs\\django', 'C:\\Users\\heast\\AppData\\Roaming\\Python\\Python36\\site-packages', 'C:\\miniconda3\\envs\\django\\lib\\site-packages', 'C:\\Program Files\\JetBrains\\PyCharm ' '2017.3.3\\helpers\\pycharm_matplotlib_backend'] |
| Server time: | Mon, 16 Jul 2018 11:16:38 -0400 |
Environment:
Request Method: GET
Django Version: 2.0.5
Python Version: 3.6.5
Installed Applications:
['item.apps.ItemConfig',
'inventory.apps.InventoryConfig',
'fulfillment.apps.FulfillmentConfig',
'manifest.apps.ManifestConfig',
'order.apps.OrderConfig',
'utils.apps.UtilsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\miniconda3\envs\django\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "C:\miniconda3\envs\django\lib\site-packages\django\core\handlers\base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "C:\miniconda3\envs\django\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\miniconda3\envs\django\lib\site-packages\django\views\generic\base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "C:\miniconda3\envs\django\lib\site-packages\django\views\generic\base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "C:\miniconda3\envs\django\lib\site-packages\django\views\generic\edit.py" in get
168. return super().get(request, *args, **kwargs)
File "C:\miniconda3\envs\django\lib\site-packages\django\views\generic\edit.py" in get
133. return self.render_to_response(self.get_context_data())
File "C:\WMS Repository\Warehouse Management System\utils\views.py" in get_context_data
93. context = super().get_context_data()
File "C:\miniconda3\envs\django\lib\site-packages\django\views\generic\edit.py" in get_context_data
66. kwargs['form'] = self.get_form()
File "C:\miniconda3\envs\django\lib\site-packages\django\views\generic\edit.py" in get_form
33. return form_class(**self.get_form_kwargs())
File "C:\WMS Repository\Warehouse Management System\utils\views.py" in get_form_kwargs
62. clone = kwargs['data'].copy()
Exception Type: KeyError at /utils/box-optimization/add-items/115
Exception Value: 'data'
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d1cddae6-72a7-46b3-a6b1-40fcdc22ae93%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment