support updating models on the many side of the one-to-many relations.
This commit is contained in:
@ -70,6 +70,12 @@ class ModelTableProxy:
|
|||||||
f"model without pk set!"
|
f"model without pk set!"
|
||||||
)
|
)
|
||||||
model_dict[field] = pk_value
|
model_dict[field] = pk_value
|
||||||
|
elif isinstance(field_value, list):
|
||||||
|
targets = [target.get(target_pkname) for target in field_value]
|
||||||
|
if targets:
|
||||||
|
model_dict[field] = targets
|
||||||
|
else:
|
||||||
|
model_dict.pop(field)
|
||||||
elif field_value: # nested dict
|
elif field_value: # nested dict
|
||||||
model_dict[field] = field_value.get(target_pkname)
|
model_dict[field] = field_value.get(target_pkname)
|
||||||
else:
|
else:
|
||||||
|
|||||||
@ -376,3 +376,16 @@ async def test_wrong_model_passed_as_fk():
|
|||||||
with pytest.raises(RelationshipInstanceError):
|
with pytest.raises(RelationshipInstanceError):
|
||||||
org = await Organisation.objects.create(ident="ACME Ltd")
|
org = await Organisation.objects.create(ident="ACME Ltd")
|
||||||
await Track.objects.create(album=org, title="Test1", position=1)
|
await Track.objects.create(album=org, title="Test1", position=1)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_bulk_update_model_with_children():
|
||||||
|
async with database:
|
||||||
|
async with database.transaction(force_rollback=True):
|
||||||
|
album = await Album.objects.create(name="Test")
|
||||||
|
track = await Track.objects.create(album=album, title="Test1", position=1)
|
||||||
|
album.name = "Test2"
|
||||||
|
await Album.objects.bulk_update([album], columns=["name"])
|
||||||
|
|
||||||
|
updated_album = await Album.objects.get(id=album.id)
|
||||||
|
assert updated_album.name == "Test2"
|
||||||
|
|||||||
Reference in New Issue
Block a user