-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes: #18833 Inventory Item Bulk Import - 'InventoryItemImportForm' has no field named 'component_id'. #18874
Conversation
About the two remaining revisions: When Django runs full_clean() in I tryed to edit the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get a ValueError exception when attempting to import with a component_type
but no component_name
specified: 'InventoryItemImportForm' has no field named 'component_id'.
Test data:
device,name,status,component_type
dmi01-akron-rtr01,item1,active,dcim.interface
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @renatoalmeidaoliveira!
Fixes: #18833 Inventory Item Bulk Import - 'InventoryItemImportForm' has no field named 'component_id'.
Root Cause:
In Django's
BaseModelForm
_post_clean
method, an instance of the model is created with data fromself.cleaned_data
. After the model instantiation, thefull_clean()
method is executed on that model instance.Since
component_type
is a field of theInventoryItem
model, the execution offull_clean()
on the instance was failing without being captured by any form validation method.Solution:
In Django's form validation process, the
clean_<fieldname>()
methods are executed before theclean_form()
method. Inside theclean_<fieldname>()
method, there are no guarantees about which field names have already been processed and is available atself.cleaned_data
.And to fully validatecomponent_type
use cases, thedevice
,component_type
, andcomponent_name
fields must be available.To fix the validation process, the
clean_component_name
method was replaced by theclean
method, and the fieldscomponent_name
andcomponent_type
were removed fromcleaned_data
when incorrect data was provided.