class ItemNumField(forms.CharField):
def validate(self, value):
# item = Item.objects.get(item_number=value)
# value = item.id
# return value
return Item.objects.get(item_number=value)
class AddItem(forms.ModelForm):
item = ItemNumField()
class Meta:
model = ItemsTestCollection
# fields = ['item', 'qty', 'case']
fields = ['item', 'qty', 'case']
class ItemsTestCollection(models.Model):
"""An item to be tested in Box Opt."""
item = models.ForeignKey('item.Item', on_delete=models.CASCADE)
qty = models.IntegerField()
case = models.ForeignKey('EvaluateTestCase', on_delete=models.CASCADE)
box = models.ForeignKey('BoxResults', on_delete=models.CASCADE, null=True)
ValueError at /utils/box-optimization/add-items/118
Cannot assign "'515874'": "ItemsTestCollection.item" must be a "Item" instance.
| Request Method: | POST |
|---|---|
| Request URL: | http://localhost:8000/utils/box-optimization/add-items/118 |
| Django Version: | 2.0.5 |
| Exception Type: | ValueError |
| Exception Value: | Cannot assign "'515874'": "ItemsTestCollection.item" must be a "Item" instance. |
| Exception Location: | C:\miniconda3\envs\django\lib\site-packages\django\db\models\fields\related_descriptors.py in __set__, line 197 |
| 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 14:58:30 -0400 |
On Monday, July 16, 2018 at 1:14:41 PM UTC-4, Matthew Pava wrote:
This StackOverflow question may help you:
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...@googlegroups.com [mailto:django...@
googlegroups.com ] On Behalf Of clavie...@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 [mailto:django...@
googlegroups.com ] On Behalf Of clavie...@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) 'item'] = str(actual_item.id)
clone[
# 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...@googlegroups.com .
To post to this group, send email to djang...@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 .--
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 .
To post to this group, send email to djang...@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
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+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/ebb14010-ff30-4df7-a90f-8d1d9b577efb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment