Skip to content

Commit 25e96a8

Browse files
committed
Check for many-to-many when setting data.
1 parent 7ae4e9b commit 25e96a8

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

django_unicorn/views/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ def set_property_from_data(
6060
set_property_from_data(field, key, key_value)
6161
else:
6262
set_property_from_data(field, field.name, value)
63+
elif hasattr(field, "related_val"):
64+
# Use `related_val` to check for many-to-many
65+
field.set(value)
6366
else:
6467
type_hints = get_type_hints(component_or_field)
6568
type_hint = type_hints.get(name)

tests/views/utils/test_set_property_from_data.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,12 @@ def test_set_property_from_data_all_querysets():
163163
component_queryset_field_asserts(component, "queryset_with_none")
164164
component_queryset_field_asserts(component, "queryset_with_empty_queryset")
165165
component_queryset_field_asserts(component, "queryset_with_no_typehint")
166+
167+
168+
@pytest.mark.django_db
169+
def test_set_property_from_data_many_to_many():
170+
component = FakeComponent(component_name="test", component_id="12345678")
171+
component.model.pk = 1
172+
173+
# No `TypeError: Direct assignment to the reverse side of a many-to-many set is prohibited.` error gets raised
174+
set_property_from_data(component.model, "taste_set", [])

0 commit comments

Comments
 (0)