-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Fixes: #18833 Inventory Item Bulk Import - 'InventoryItemImportForm' has no field named 'component_id'. #18874
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
Conversation
|
About the two remaining revisions: When Django runs full_clean() in I tryed to edit the |
jeremystretch
left a comment
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
jeremystretch
left a comment
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_cleanmethod, 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_typeis a field of theInventoryItemmodel, 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_typeuse cases, thedevice,component_type, andcomponent_namefields must be available.To fix the validation process, the
clean_component_namemethod was replaced by thecleanmethod, and the fieldscomponent_nameandcomponent_typewere removed fromcleaned_datawhen incorrect data was provided.