Created
May 3, 2018 14:56
-
-
Save kulor/6cccca5242f767356fba37b852afa271 to your computer and use it in GitHub Desktop.
Example idempotent resource creation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_inventory_item(request): | |
body_unicode = request.body.decode('utf-8') | |
body = json.loads(body_unicode) | |
idempotent_id = body.get('idempotent_id') | |
item = InventoryModel.objects.get(idempotent_id=idempotent_id) | |
if item: | |
return JsonResponse(item) # Will include the backend primary key ID | |
else: | |
InventoryModel.objects.create( | |
idempotent_id=idempotent_id, # Stored in the DB | |
name=body.get('name') | |
... | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment