Skip to content

Instantly share code, notes, and snippets.

@kulor
Created May 3, 2018 14:56
Show Gist options
  • Save kulor/6cccca5242f767356fba37b852afa271 to your computer and use it in GitHub Desktop.
Save kulor/6cccca5242f767356fba37b852afa271 to your computer and use it in GitHub Desktop.
Example idempotent resource creation
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