This StackOverflow question may help you: https://www.google.com/search?q=django+clean_fields+not+called&oq=django+clean_field+not+&aqs=chrome.1.69i57j0.7599j0j4&sourceid=chrome&ie=UTF-8
Basically this: For any field, if the Field.clean() method raises a ValidationError, any field-specific cleaning method is not called. However, the cleaning methods for all remaining fields are still executed. item should already return an instance of an Item object without your clean method. From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On Behalf Of clavierpla...@gmail.com Sent: Monday, July 16, 2018 11:27 AM To: Django users Subject: Re: help with get_form_kwargs I would actually prefer to override clean(), but CreateView doesn't appear to have a clean() method that I can override. I did make a ModelForm to use with the CreateView, but the clean_item method I defined there never gets called--breakpoints and print statements are all skipped, so I don't even have any stack trace information because nothing actually breaks. I've looked through the source code and I still don't see how CreateView cleans its data. class AddItem(forms.ModelForm): class Meta: model = ItemsTestCollection fields = ['item', 'qty', 'case'] def clean_item(self): print("will it print anything?") # Answer: nope, didn't print anything. item_num = self.cleaned_data['item'] actual_item = Item.objects.get(item_number=item_num) return actual_item.id I'm sure I've overlooked something simple, but meanwhile I'm looking for any kind of workaround. But thank you for the suggestion! I wish I knew how to implement it. On Monday, July 16, 2018 at 11:46:39 AM UTC-4, Matthew Pava wrote: I would alter user input in the clean methods. https://docs.djangoproject.com/en/2.0/ref/forms/validation/ From: django...@googlegroups.com<javascript:> [mailto:django...@googlegroups.com<javascript:>] On Behalf Of clavie...@gmail.com<javascript:> 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<http://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: http://localhost:8000/utils/box-optimization/add-items/115 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 Request URL: http://localhost:8000/utils/box-optimization/add-items/115 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...@googlegroups.com<javascript:>. To post to this group, send email to djang...@googlegroups.com<javascript:>. 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<https://groups.google.com/d/msgid/django-users/d1cddae6-72a7-46b3-a6b1-40fcdc22ae93%40googlegroups.com?utm_medium=email&utm_source=footer>. For more options, visit https://groups.google.com/d/optout. -- 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+unsubscr...@googlegroups.com<mailto:django-users+unsubscr...@googlegroups.com>. To post to this group, send email to django-users@googlegroups.com<mailto: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/e7b64bc4-8c00-4db8-aada-e3f2a4751b7e%40googlegroups.com<https://groups.google.com/d/msgid/django-users/e7b64bc4-8c00-4db8-aada-e3f2a4751b7e%40googlegroups.com?utm_medium=email&utm_source=footer>. For more options, visit https://groups.google.com/d/optout. -- 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+unsubscr...@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/4e8914f9391e4018a394c46a5ceff433%40ISS1.ISS.LOCAL. For more options, visit https://groups.google.com/d/optout.