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

ぐ巨炮叔叔 提交于 2020-05-15 06:04:17

问题


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?


回答1:


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




回答2:


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.




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/55976471/how-can-i-use-awss-dynamo-db-with-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!