Skip to content

[fix] Fixed MultiValueDictKeyError on empty device form submission#1173

Merged
nemesifier merged 6 commits intoopenwisp:masterfrom
Srinath0916:fix-multivaluedictkeyerror-1057
Dec 19, 2025
Merged

[fix] Fixed MultiValueDictKeyError on empty device form submission#1173
nemesifier merged 6 commits intoopenwisp:masterfrom
Srinath0916:fix-multivaluedictkeyerror-1057

Conversation

@Srinath0916
Copy link
Contributor

@Srinath0916 Srinath0916 commented Dec 8, 2025

Checklist

  • I have read the OpenWISP Contributing Guidelines.
  • I have manually tested the changes proposed in this pull request.
  • I have written new test cases for new code and/or updated existing tests for changes to existing code.
  • N/A I have updated the documentation.

Reference to Existing Issue

Closes #1057

Description of Changes

Fixed the MultiValueDictKeyError that happened when you click "Add another Configuration" on a new device and hit save without filling anything in.

The problem was in the ConfigForm.get_temp_model_instance() method - it was trying to access organization, name, and mac_address from the form data without checking if they exist first. Changed it to use .get() so it handles empty fields properly and shows validation errors instead of crashing.

Added a test case to verify the fix works.

Screenshot

See issue #1057 for the error screenshot. After the fix, the form now shows proper validation errors instead of crashing.

@Srinath0916 Srinath0916 force-pushed the fix-multivaluedictkeyerror-1057 branch from ee88b78 to 76cb583 Compare December 8, 2025 19:07
config = self._create_config(organization=self._get_org())
self._verify_template_queries(config, 10)

def test_empty_device_form_with_config_inline(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please try running this test without the fix and check if it fails or not?
Also I don't think it is necessary to mention the issue in the docstring, a simple description of what it does is enough, you can take other tests as reference

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @DragnEmperor , Thanks for the feedback! I've updated the test to properly reproduce the bug by omitting the device fields from the POST data, which matches the actual scenario from the issue.

I tried running it locally but hit a Twisted import error with my Python version. However, the test logic is clear: without the fix, accessing self.data["organization"] on a missing key raises MultiValueDictKeyError, and with the fix using .get(), it's handled gracefully.

CI should catch it if there's any issue. Let me know if you need anything else changed.

Also updated the docstring as you suggested.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @DragnEmperor , I've fixed the CI issues from the previous run. The workflow needs approval to run the tests again. Could you approve it when you get a chance?

@Srinath0916 Srinath0916 force-pushed the fix-multivaluedictkeyerror-1057 branch 2 times, most recently from 19170b9 to 21c1fb1 Compare December 9, 2025 15:50
@Srinath0916
Copy link
Contributor Author

Hey @DragnEmperor , really sorry for the inconvenience, I had to fix the commit message format to match the project guidelines (added the [fix] prefix). Could you approve the workflow one more time?
should be good to go now.

Comment on lines 97 to 103
if: ${{ failure() && steps.deps.conclusion == 'success' }}
run: |
if [ -f geckodriver.log ]; then
cat geckodriver.log
else
echo "geckodriver.log not found"
fi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this change required? IMO any unrelated changes should be taken up if there is a dedicated issue for the same. Else, would be difficult to track the changes.

Copy link
Contributor Author

@Srinath0916 Srinath0916 Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, my bad! I added this while debugging some test failures but it's not really needed for this PR. Removed it in the latest commit. Thank you for addressing

Regarding the checks - all of them should pass except Python 3.9. This seems to be an existing issue on the main branch and not related to my changes. I encountered the same Python 3.9 failure in my previous PR as well, and it was also discussed in the community group.

I've opened PR #1174 to drop Python 3.9 support from openwisp-controller, which should resolve this CI issue for all pending PRs once merged.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @DragnEmperor , PR #1174 has been merged which drops Python 3.9 support. Could you re-run the checks on this PR? All tests should pass now. Thanks!

Fixes openwisp#1057

Fixed MultiValueDictKeyError when submitting empty device form
with configuration inline. Changed direct dictionary access to
use .get() method with defaults to allow proper validation.
@Srinath0916 Srinath0916 force-pushed the fix-multivaluedictkeyerror-1057 branch from e0f61e4 to d152703 Compare December 10, 2025 10:52
@Srinath0916 Srinath0916 force-pushed the fix-multivaluedictkeyerror-1057 branch from d152703 to 94f9cd2 Compare December 10, 2025 11:06
@coveralls
Copy link

coveralls commented Dec 10, 2025

Coverage Status

coverage: 98.608% (-0.003%) from 98.611%
when pulling c0096f2 on Srinath0916:fix-multivaluedictkeyerror-1057
into 03ddc6c on openwisp:master.

@Srinath0916
Copy link
Contributor Author

Hi @pandafy , @DragnEmperor , all checks are passing now, This PR is ready for review.

@DragnEmperor
Copy link
Member

Hi @pandafy , @DragnEmperor , all checks are passing now, This PR is ready for review.

Hi @Srinath0916 , I would suggest to have a look once again through all the changes done to ensure any unrelated changes are not there. I see some changes in workflow yaml file.

@Srinath0916
Copy link
Contributor Author

Thanks @DragnEmperor ! I've reviewed all changes in this PR,

The MultiValueDictKeyError fix includes: - config/admin.py (the main fix) - config/base/config.py (related changes) - config/tests/test_admin.py (test for the fix)

The workflow yaml changes include: - Geckodriver logging removal (as you said) and Python 3.9 related changes from updating with master (PR #1174)

Should I clean up the workflow file to keep only the geckodriver removal you asked for and remove the Python 3.9 changes? I want to make sure the tests still pass."

@DragnEmperor
Copy link
Member

Thanks @DragnEmperor ! I've reviewed all changes in this PR,

The MultiValueDictKeyError fix includes: - config/admin.py (the main fix) - config/base/config.py (related changes) - config/tests/test_admin.py (test for the fix)

The workflow yaml changes include: - Geckodriver logging removal (as you said) and Python 3.9 related changes from updating with master (PR #1174)

Should I clean up the workflow file to keep only the geckodriver removal you asked for and remove the Python 3.9 changes? I want to make sure the tests still pass."

@Srinath0916 I meant to keep the ci.yml as it is, no changes at all, currently I see there is some removal of code

@Srinath0916
Copy link
Contributor Author

Thanks @DragnEmperor ! I've reverted ci.yml to match master exactly. Now the PR contains only the MultiValueDictKeyError fix files with no workflow changes. Ready for review!

DragnEmperor
DragnEmperor previously approved these changes Dec 10, 2025
@github-project-automation github-project-automation bot moved this from Needs review to In progress in OpenWISP Contributor's Board Dec 10, 2025
@DragnEmperor
Copy link
Member

LGTM

@Srinath0916
Copy link
Contributor Author

Thank you @DragnEmperor , Really appreciate your patience and detailed feedback throughout this process.

@Srinath0916
Copy link
Contributor Author

Hi @pandafy , This PR has been approved, and all checks are passing. Could you help merge it when you get a chance? Thanks.

Comment on lines +375 to +380
if not config:
# The request does not contain vaild data to create a temporary
# Device instance. Thus, we cannot validate the templates.
# The Device validation will be handled by DeviceAdmin.
# Therefore, we don't need to raise any error here.
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shall return early if the data does not contain organization.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @pandafy , for the review and the improvement,
Really appreciate the feedback and approval.

pandafy
pandafy previously approved these changes Dec 12, 2025
Copy link
Member

@pandafy pandafy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made a small improvement in 81f0780.

LGTM!

@pandafy pandafy moved this from In progress to Needs review in OpenWISP Contributor's Board Dec 12, 2025
@nemesifier nemesifier added the bug label Dec 19, 2025
@nemesifier nemesifier changed the title Fix MultiValueDictKeyError on empty device form submission 1057 [fix] Fixed MultiValueDictKeyError on empty device form submission Dec 19, 2025
# Should show validation errors instead
self.assertEqual(response.status_code, 200)
self.assertContains(response, "errorlist")
# Verify no device was created
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid adding too many comments, it's not useful, I am going to remove some.

@nemesifier nemesifier force-pushed the fix-multivaluedictkeyerror-1057 branch from 97b9ddd to c0096f2 Compare December 19, 2025 17:54
@github-project-automation github-project-automation bot moved this from Needs review to In progress in OpenWISP Contributor's Board Dec 19, 2025
@nemesifier nemesifier merged commit 828dfb3 into openwisp:master Dec 19, 2025
13 checks passed
@github-project-automation github-project-automation bot moved this from In progress to Done in OpenWISP Contributor's Board Dec 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

[bug] Uncaught exception: MultiValueDictKeyError at /admin/config/device/add/

5 participants