Skip to content

fix: allow setting a list as a field value#21

Merged
GitRon merged 3 commits intoambient-innovation:mainfrom
JBthePenguin:JBL/allow_setting_list_as_value
Mar 8, 2026
Merged

fix: allow setting a list as a field value#21
GitRon merged 3 commits intoambient-innovation:mainfrom
JBthePenguin:JBL/allow_setting_list_as_value

Conversation

@JBthePenguin
Copy link
Copy Markdown
Contributor

To set form data, use the setlist method of QueryDict when the provided value is Iterable.

@GitRon
Copy link
Copy Markdown
Contributor

GitRon commented Feb 6, 2026

Hi @JBthePenguin

the code looks good. Would you mind adding at least one test for the changes?

Best regards
Ronny

@GitRon GitRon self-requested a review February 6, 2026 09:08
@JBthePenguin
Copy link
Copy Markdown
Contributor Author

Hi @GitRon !
OK, I will add a test asap. The goal is to have 100% coverage, right?

@GitRon
Copy link
Copy Markdown
Contributor

GitRon commented Feb 6, 2026

@JBthePenguin That would be awesome. But having tests to ensure the new functionality works and keeps working is important, too 🙂

@JBthePenguin
Copy link
Copy Markdown
Contributor Author

@GitRon Test added.

Copy link
Copy Markdown
Contributor

@GitRon GitRon left a comment

Choose a reason for hiding this comment

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

Hi @JBthePenguin! Thanks for the tests. I've asked the AI for a second opinion since I've took over this package and I'm not the one that built it in the first place.

What do you think?

bound_field.field.queryset = queryset
bound_field.form.data = bound_field.form.data.copy()
bound_field.form.data[field_name] = value
if isinstance(value, Iterable):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

issue: I've asked the AI for a second opinion. Seems legit, doesn't it?

Potential Issue: str is Iterable

In Python, str is an Iterable. If a get_dynamic_*_field method returns a string as value, it will be passed to .setlist(), which would split the string into individual characters — almost certainly not the intended behavior.

For example, if value = "hello", then setlist(field_name, "hello") would set the field to ["h", "e", "l", "l", "o"].

The fix should explicitly exclude strings:

if isinstance(value, Iterable) and not isinstance(value, str):

Or alternatively, check specifically for list/tuple:

if isinstance(value, (list, tuple)):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@GitRon Fix done.

with self.assertRaises(PermissionDenied):
mixin.render_field(request)

def test_render_field_with_custom_method_for_many_to_many_field(self):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

issue: Another valid AI suggestin IMHO

The new test (test_render_field_with_custom_method_for_many_to_many_field) verifies the happy path returns a 200 status. It's a reasonable integration-style test, but could be strengthened by:

  1. Asserting the response body contains the expected widget HTML with the pre-selected values ("1" and "2").
  2. Adding a test that a non-list value (e.g., a string) still works correctly after this change — to catch the str is Iterable edge case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@GitRon Thanks for comments! That seems legit, yes. I will post an update as soon as I find a moment to do so.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You're the best ❤️

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@GitRon Two tests added. The first one to check if setlist method is called when value is a list, and the second one to check if setlist method is not called when value is a str.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks! ❤️ Will have a look once I find a minute 😅

Copy link
Copy Markdown
Contributor

@GitRon GitRon left a comment

Choose a reason for hiding this comment

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

Great, thanks! 👌

@GitRon GitRon merged commit c4d96f8 into ambient-innovation:main Mar 8, 2026
14 checks passed
@GitRon
Copy link
Copy Markdown
Contributor

GitRon commented Mar 8, 2026

@JBthePenguin Thanks for the work! Just published the new release: https://pypi.org/project/django-dynamic-admin-forms

@JBthePenguin
Copy link
Copy Markdown
Contributor Author

@GitRon Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants