この記事の内容は、Django 開発における mongodb の設定と使用法に関するものであり、一定の参考価値がありますので、困っている方は参考にしていただければ幸いです。
今日はdjangoプロジェクトでmongodbを使う方法を整理しました環境はubuntu18.04, django2.0.5, drf3.9, mongoengine0.16
最初のステップ: settings.py で mongodb と mysql を設定します。設定は次のとおりです (mysql と mongodb は同時に使用できます):
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', # 数据库引擎 'NAME': 'django_test2', # 你要存储数据的库名,事先要创建之 'USER': 'root', # 数据库用户名 'PASSWORD': 'wyzane', # 密码 'HOST': 'localhost', # 主机 'PORT': '3306', # 数据库使用的端口 }, 'mongotest': { 'ENGINE': None, } } import mongoengine # 连接mongodb中数据库名称为mongotest5的数据库 conn = mongoengine.connect("mongotest")
ステップ 2: mongodb にデータを挿入
1. json 型データを挿入
models.py: import mongoengine class StudentModel(mongoengine.Document): name = mongoengine.StringField(max_length=32) age = mongoengine.IntField() password = mongoengine.StringField(max_length=32) views.py: from rest_framework.views import APIView class FirstMongoView(APIView): def post(self, request): name = request.data["name"] age = request.data["age"] password = request.data["password"] StudentModel.objects.create(name=name, age=age, password=password) return Response(dict(msg="OK", code=10000))
挿入されるデータの形式は次のとおりです:
{ "name": "nihao", "age": 18, "password": "123456" }
2. リストを含む json データを挿入します##
models.py: import mongoengine class Student2Model(mongoengine.Document): name = mongoengine.StringField(max_length=32) # 用于存储list类型的数据 score = mongoengine.ListField() views.py: from rest_framework.views import APIView class FirstMongo2View(APIView): def post(self, request): name = request.data["name"] score = request.data["score"] Student2Model.objects.create(name=name, score=score) return Response(dict(msg="OK", code=10000))
{ "name": "test", "score": [12, 13] }
models.py: import mongoengine class Student3Model(mongoengine.Document): name = mongoengine.StringField(max_length=32) # DictField用于存储字典类型的数据 score = mongoengine.DictField() views.py: from rest_framework.views import APIView class FirstMongo3View(APIView): def post(self, request): name = request.data["name"] score = request.data["score"] Student3Model.objects.create(name=name, score=score) return Response(dict(msg="OK", code=10000))
{ "name": "test", "score": {"xiaoming": 12, "xiaoli": 13} } 或者: { "name": "test", "score": {"xiaoming": 12, "xiaoli": {"xiaozhao": 14}} } 或者: { "name": "test", "score": {"xiaoming": 12, "xiaoli": {"xiaozhao": {"xiaoliu": 12, "xiaojian": 18}}} } 或者: { "name": "test", "score": {"xiaoming": 12, "xiaoli": {"xiaozhao": {"xiaoliu": 12, "xiaojian": [12,13,14]}}} }
serializers.py: class StudentSerializer(serializers.Serializer): name = serializers.CharField() score = serializers.DictField() # 序列化复杂的json数据 # DictField与EmbeddedDocumentField类似,但是比EmbeddedDocumentField更灵活 views.py: class FirstMongo4View(APIView): def get(self, request): student_info = Student3Model.objects.all() # 增加过滤条件 # student_info = Student3Model.objects.filter(name="test1") ser = StudentSerializer(instance=student_info, many=True) return Response(dict(msg="OK", code="10000", data=ser.data))
models.py: class AuthorModel(mongoengine.EmbeddedDocument): author_name = mongoengine.StringField(max_length=32) age = mongoengine.IntField() class BookModel(mongoengine.Document): book_name = mongoengine.StringField(max_length=64) publish = mongoengine.DateTimeField(default=datetime.datetime.utcnow()) words = mongoengine.IntField() author = mongoengine.EmbeddedDocumentField(AuthorModel) serializers.py: 序列化时注意与rest_framework的序列化中DictField()的区别 from rest_framework_mongoengine import serializers as s1 class AuthorSerializer(s1.DocumentSerializer): # DocumentSerializer继承自drf中的ModelSerializer,用于代替ModelSerializer序列化mongodb中的document. # 具体可以到官网上查看 class Meta: model = AuthorModel fields = ('author_name', 'age') class BookSerializer(s1.DocumentSerializer): author = AuthorSerializer() class Meta: model = BookModel fields = ('book_name', 'publish', 'words', 'author') AuthorSerializer还可以这样写: class AuthorSerializer(s1.EmbeddedDocumentSerializer): # EmbeddedDocumentSerializer继承了DocumentSerializer class Meta: model = AuthorModel fields = ('author_name', 'age') views.py: class BookView(APIView): def get(self, request): """ 查询数据 :param request: :return: """ books = BookModel.objects.all() ser = BookSerializer(instance=books, many=True) return Response(dict(msg="OK", code="10000", data=ser.data))
Django serialization to JSON error: 'MetaDict' object has no attribute 'concrete_model'
http://umutbozkurt.github.io/...
以上がDjango 開発のための mongodb の構成と使用の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。