今天整理了一下在django專案中如何使用mongodb, 環境如下:
ubuntu18.04, django2.0.5, drf3.9, mongoengine0.16
第一步:在settings.py中配置mongodb和mysql,配置如下(可以同時使用mysql和mongodb):
databases = ,
'mongotest':
}import mongoengine
# 連線mongodb中資料庫名稱為mongotest5的資料庫
conn = mongoengine.connect("mongotest")
第二步:向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))
插入資料格式為:
2.插入含有list的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))
插入資料格式為:
3.插入含有dict和list的複雜json資料
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))
插入資料格式為:
}或者:}}
或者:}}
}或者:
}}}
第三步:查詢mongodb中的資料
1.查詢並序列化複雜json資料
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))
2.序列化mongodb中含有巢狀關係的兩個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: 序列化時注意與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'
此時,序列化器需要繼承自rest_framework_mongoengine的類,具體可以檢視官網:
參考:
django使用mysql開發 Linux
一 安裝mysql pip install mysql python 二 登入mysql 命令 mysql uroot p mysql預設密碼為mysql 三 檢視資料庫列表show databases 刪除不要的資料庫drop 資料庫名稱 開啟use 資料庫名稱 建立資料庫create datab...
在Django中使用mysql
在django中使用mongodb 1 可以選擇虛擬環境,進入開發環境的虛擬空間,不知道的請看傳送門 2 基本包的版本 django 1.11.8 mongoengine 0.15.0 3 安裝包 pip install mysqlclient4 建立乙個新的django專案,並指定到虛擬空間的py...
Django中mysql的使用
首先需要安裝mysqlclient pip install mysqlclient然後在mysql資料庫中建立好資料庫跟表 安裝完成之後,修改專案的settings.py檔案中資料庫的配置項 資料配置檔案 databases 然後在應用中的views.py中使用django自帶的connection...