Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use AWS's Dynamo Db with Django?

I am developing web applications, APIs, and backends using the Django MVC framework. A major aspect of Django is its implementation of an ORM for models. It is an exceptionally good ORM. Typically when using Django, one utilizes an existing interface that maps one's Django model to a specific DBMS like Postgres, MySQL, or Oracle for example.

I have some specific needs, requirements regarding performance and scalability, so I really want to use AWS's Dynamo DB because it is highly cost efficient, very performant, and scales really well.

While I think Django allows one to implement their own interface for a DBMS if one wishes to do so, it is clearly advantageous to be able to use an existing DBMS interface when constructing one's Django models if one exists.

Can someone recommend a Django model interface to use so I can construct a model in Django that uses AWS's Dynamo DB?

How about one using MongoDB?

like image 567
Peter Jirak Eldritch Avatar asked May 03 '19 20:05

Peter Jirak Eldritch


2 Answers

As written by others, Django does not have NoSQL DBMS. There are third-party packages, but given the flexible nature of NoSQL data, no ‘ready-made batteries’, as @slajma said.

PynamoDB seems fine, I never used it, so I can’t recommend. 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 db and perform operation you want (boto3)
  2. parse incoming data into 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 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)
        ...

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

like image 112
Pawel Kam Avatar answered Oct 21 '22 01:10

Pawel Kam


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

like image 26
slajma Avatar answered Oct 20 '22 23:10

slajma