本篇文章给大家带来的内容是关于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)
第二步:向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、插入含有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))
插入数据格式为:
{ name: test, score: [12, 13]}
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))
插入数据格式为:
{ 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中的数据
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))
序列化mongodb中相关联的两个表时,如果序列化器继承自rest_framework中的serializer和modelserializer,会抛出如下异常:
django serialization to json error: 'metadict' object has no attribute 'concrete_model'
此时,序列化器需要继承自rest_framework_mongoengine的类,具体可以查看官网:
http://umutbozkurt.github.io/...
以上就是django开发之mongodb的配置与使用的详细内容。