[Fixed]-How can I use AWS's Dynamo Db with Django?

12👍

As written by others, Django does not have NoSQL DBMS support, but there are third-party packages.

PynamoDB seems fine, but I have never used it, so I can’t recommend it. In all use cases I came across, boto3 was sufficient. Setup is pretty simple, but the devil is in details (in the data structure and how nested it is, to be precise). Basically, three steps are needed:

  1. Connect with the DB and perform the operation you want (boto3)
  2. Parse incoming data into a Python dictionary (e.g. with dynamodb-json, boto3.dynamodb.types.TypeDeserializer or you can build your own)
  3. Do business logic, store data into relational DB using the Django ORM or whatever you need

Simplest example:

from dynamodb_json import json_util as dynamodb_json
from .models import YourModel

def get(request, partition_key):
    table = boto3.resource(
        'dynamodb',
        aws_access_key_id=...,
        aws_secret_access_key=...,
        region_name=...,
    ).Table(some_table_name)
    try:
        response = table.get_item(
            Key={partition_key: partition_key})
    except ClientError as e:
        logger.warning(e.response['Error']['Message'])
    else:
        data_str = response['Item']
        _data_dict = dynamodb_json.loads(data_str)

        # Validation and modification of incoming data goes here.
        data_dict = validation_and_modification(_data_dict)
        # Then you can do whatever you need, for example:
        obj, created = YourModel.objects.update_or_create(**data_dict)
        ...

Examples for create, delete, list and update views can be found in the serverless repo.

4👍

It’s not like ready made battery for django, but worth looking at it regardless.
https://github.com/pynamodb/PynamoDB

👤slajma

2👍

You can try DynamORM or PynamoDB. I haven’t tried them maybe they can help.

1👍

DynamoDB is non-relational which I think makes it architecturally incompatible with an ORM like Django’s.

👤rcx935

0👍

There is no Django model interface for AWS DynamoDB, but you may retrieve data from that kind of db using boto3 software provided by AWS.

👤Zhigal

Leave a comment