Skip to content

Commit

Permalink
client generated ids (fixes #72)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaxxl committed Aug 5, 2020
1 parent f3fbf51 commit b3512db
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 1 addition & 2 deletions safrs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def _s_parse_attr_value(self, attr_name, attr_val):
return parse_attr(attr, attr_val)

@classmethod
def _s_post(cls, **attributes):
def _s_post(cls, id=None, **attributes):
"""
This method is called when a new item is created with a POST to the json api
Expand All @@ -236,7 +236,6 @@ def _s_post(cls, **attributes):

if getattr(cls, "allow_client_generated_ids", False) is True:
# todo, this isn't required per the jsonapi spec, doesn't work well and isn't documented, maybe later
id = attributes.get("data", {}).get("id")
# cls.id_type.get_pks(id)
attributes["id"] = id

Expand Down
4 changes: 4 additions & 0 deletions safrs/jsonapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,10 @@ def _create_instance(self, data):
raise ValidationError("Invalid type member: {} != {}".format(obj_type, self.SAFRSObject._s_type))

attributes = data.get("attributes", {})
if self.SAFRSObject.allow_client_generated_ids:
client_generated_id = data.get("id", None)
attributes["id"] = client_generated_id

instance = self.SAFRSObject._s_post(**attributes)

return instance
Expand Down

0 comments on commit b3512db

Please sign in to comment.