摘要:今天整理了一下在項(xiàng)目中如何使用環(huán)境如下第一步在中配置和配置如下可以同時(shí)使用和數(shù)據(jù)庫(kù)引擎你要存儲(chǔ)數(shù)據(jù)的庫(kù)名,事先要?jiǎng)?chuàng)建之?dāng)?shù)據(jù)庫(kù)用戶名密碼主機(jī)數(shù)據(jù)庫(kù)使用的端口連接中數(shù)據(jù)庫(kù)名稱為的數(shù)據(jù)庫(kù)第二步向中插入數(shù)據(jù)插入類型數(shù)據(jù)插入數(shù)據(jù)格式為插入含有的數(shù)據(jù)用
今天整理了一下在django項(xiàng)目中如何使用mongodb, 環(huán)境如下:
ubuntu18.04, django2.0.5, drf3.9, mongoengine0.16
第一步:在settings.py中配置mongodb和mysql,配置如下(可以同時(shí)使用mysql和mongodb):
DATABASES = { "default": { "ENGINE": "django.db.backends.mysql", # 數(shù)據(jù)庫(kù)引擎 "NAME": "django_test2", # 你要存儲(chǔ)數(shù)據(jù)的庫(kù)名,事先要?jiǎng)?chuàng)建之 "USER": "root", # 數(shù)據(jù)庫(kù)用戶名 "PASSWORD": "wyzane", # 密碼 "HOST": "localhost", # 主機(jī) "PORT": "3306", # 數(shù)據(jù)庫(kù)使用的端口 }, "mongotest": { "ENGINE": None, } } import mongoengine # 連接mongodb中數(shù)據(jù)庫(kù)名稱為mongotest5的數(shù)據(jù)庫(kù) conn = mongoengine.connect("mongotest")
第二步:向mongodb中插入數(shù)據(jù)
1.插入json類型數(shù)據(jù)
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))
插入數(shù)據(jù)格式為:
{ "name": "nihao", "age": 18, "password": "123456" }
2.插入含有l(wèi)ist的json數(shù)據(jù)
models.py: import mongoengine class Student2Model(mongoengine.Document): name = mongoengine.StringField(max_length=32) # 用于存儲(chǔ)list類型的數(shù)據(jù) 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))
插入數(shù)據(jù)格式為:
{ "name": "test", "score": [12, 13] }
3.插入含有dict和list的復(fù)雜json數(shù)據(jù)
models.py: import mongoengine class Student3Model(mongoengine.Document): name = mongoengine.StringField(max_length=32) # DictField用于存儲(chǔ)字典類型的數(shù)據(jù) 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))
插入數(shù)據(jù)格式為:
{ "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]}}} }
第三步:查詢mongodb中的數(shù)據(jù)
1.查詢并序列化復(fù)雜json數(shù)據(jù)
serializers.py: class StudentSerializer(serializers.Serializer): name = serializers.CharField() score = serializers.DictField() # 序列化復(fù)雜的json數(shù)據(jù) # DictField與EmbeddedDocumentField類似,但是比EmbeddedDocumentField更靈活 views.py: class FirstMongo4View(APIView): def get(self, request): student_info = Student3Model.objects.all() # 增加過(guò)濾條件 # student_info = Student3Model.objects.filter(name="test1") ser = StudentSerializer(instance=student_info, many=True) return Response(dict(msg="OK", code="10000", data=ser.data))
2.序列化mongodb中含有嵌套關(guān)系的兩個(gè)document
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: 序列化時(shí)注意與rest_framework的序列化中DictField()的區(qū)別 from rest_framework_mongoengine import serializers as s1 class AuthorSerializer(s1.DocumentSerializer): # DocumentSerializer繼承自drf中的ModelSerializer,用于代替ModelSerializer序列化mongodb中的document. # 具體可以到官網(wǎng)上查看 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): """ 查詢數(shù)據(jù) :param request: :return: """ books = BookModel.objects.all() ser = BookSerializer(instance=books, many=True) return Response(dict(msg="OK", code="10000", data=ser.data))
序列化mongodb中相關(guān)聯(lián)的兩個(gè)表時(shí),如果序列化器繼承自rest_framework中的Serializer和ModelSerializer,會(huì)拋出如下異常:
Django serialization to JSON error: "MetaDict" object has no attribute "concrete_model"
此時(shí),序列化器需要繼承自rest_framework_mongoengine的類,具體可以查看官網(wǎng):
http://umutbozkurt.github.io/...
今天就聊到這里,如有問(wèn)題,歡迎交流。
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/19429.html
摘要:今天整理了一下在項(xiàng)目中如何使用環(huán)境如下第一步在中配置和配置如下可以同時(shí)使用和數(shù)據(jù)庫(kù)引擎你要存儲(chǔ)數(shù)據(jù)的庫(kù)名,事先要?jiǎng)?chuàng)建之?dāng)?shù)據(jù)庫(kù)用戶名密碼主機(jī)數(shù)據(jù)庫(kù)使用的端口連接中數(shù)據(jù)庫(kù)名稱為的數(shù)據(jù)庫(kù)第二步向中插入數(shù)據(jù)插入類型數(shù)據(jù)插入數(shù)據(jù)格式為插入含有的數(shù)據(jù)用 今天整理了一下在django項(xiàng)目中如何使用mongodb, 環(huán)境如下:ubuntu18.04, django2.0.5, drf3.9, mong...
摘要:實(shí)現(xiàn)用戶管理程序?qū)?shù)據(jù)庫(kù)中文件的增加和修改。至此,所有和相關(guān)的功能數(shù)據(jù)庫(kù)等都這樣運(yùn)作。管理器的使用和管理器一樣將會(huì)獲得一個(gè)帶有主鍵的對(duì)象。例如,對(duì)在中保存為一個(gè)數(shù)據(jù)表,在中保存為一個(gè)集合使用,函數(shù)名變?yōu)椤? 在Django項(xiàng)目中使用MongoDB作為后端數(shù)據(jù)庫(kù),且不改變Django的ORM框架。實(shí)現(xiàn)Django用戶管理程序?qū)ongoDB數(shù)據(jù)庫(kù)中文件的增加和修改。 用法 1.pip in...
摘要:實(shí)現(xiàn)用戶管理程序?qū)?shù)據(jù)庫(kù)中文件的增加和修改。至此,所有和相關(guān)的功能數(shù)據(jù)庫(kù)等都這樣運(yùn)作。管理器的使用和管理器一樣將會(huì)獲得一個(gè)帶有主鍵的對(duì)象。例如,對(duì)在中保存為一個(gè)數(shù)據(jù)表,在中保存為一個(gè)集合使用,函數(shù)名變?yōu)椤? 在Django項(xiàng)目中使用MongoDB作為后端數(shù)據(jù)庫(kù),且不改變Django的ORM框架。實(shí)現(xiàn)Django用戶管理程序?qū)ongoDB數(shù)據(jù)庫(kù)中文件的增加和修改。 用法 1.pip in...
閱讀 2953·2023-04-26 01:49
閱讀 2082·2021-10-13 09:39
閱讀 2293·2021-10-11 11:09
閱讀 936·2019-08-30 15:53
閱讀 2825·2019-08-30 15:44
閱讀 932·2019-08-30 11:12
閱讀 2992·2019-08-29 17:17
閱讀 2385·2019-08-29 16:57