Closed zvyn closed 5 months ago
Hey @zvyn ,
Is this issue fixed by https://github.com/strawberry-graphql/strawberry-graphql-django/pull/362 ? Or is there anything else that we need to do here?
Hey @bellini666,
sorry for taking so long to respond!
This issue goes a bit further than #362:
This issue proposes a way to update properties of nested objects (provided there is a primary key to identify the nested object reliably).
If that sounds good to you, we (@geops) would be happy to implement this!
If that sounds good to you, we (https://github.com/geops) would be happy to implement this!
@zvyn sure, go ahead! :)
Feel free to ping me on discord if you need anything
Hi @bellini666,
@zvyn and me will implement the feature.
Zvyn proposed to look for the id
attribute in the data
dictionary in
def _parse_pk(
value: ParsedObject | strawberry.ID | _M | None,
model: type[_M],
) -> tuple[_M | None, dict[str, Any] | None]:
if value is None:
return None, None
if isinstance(value, Model):
return value, None
if isinstance(value, ParsedObject):
return value.parse(model)
if isinstance(value, dict):
# do the changes here....
return None, value
return model._default_manager.get(pk=value), None
and in update_m2m
:
- Look for an ID in _parse_pk and
return model._default_manager.get(pk=int(value.pop("id"))), value
if set- Always create where https://github.com/strawberry-graphql/strawberry-graphql-django/pull/362 switched to
get_or_create
if the ID is optional and UNSET (but leave the behaviour unchanged for related input types without an optional ID)
However, it is possible to change the unique identifier, which is stored as self.key_attr
in the DjangoCreateOrUpdateMutation
class. Hardcoding to id
would just cover the case of looking for objects via their id
as their pk
. Do you know a generic way without changing the API of the update_m2m
method and its calling functions?
Thanks in advance! tokr-bit
@tokr-bit nice, looking forward to review the PR :)
However, it is possible to change the unique identifier, which is stored as self.key_attr in the DjangoCreateOrUpdateMutation class. Hardcoding to id would just cover the case of looking for objects via their id as their pk. Do you know a generic way without changing the API of the update_m2m method and its calling functions?
I think the easiest way is to add an extra key_attr: str | None = 'id'
to it, which sould be received by the create/update functions and propagated to update_m2m and _parse_pk
Let me know if you have any other issues. Feel free to post them either here or ping me on discord
Feature Request Type
Description
I would like to propose supporting updates on nested relations.
Proposed solution:
_parse_pk
and returnmodel._default_manager.get(pk=int(value.pop("id"))), value
if setget_or_create
if the ID is optional andUNSET
(but leave the behaviour unchanged for related input types without an optional ID)Example setup copied from #360 but
unique_together
-constraintid
field on theChild
With that the client can
What do you think?
Upvote & Fund