美文网首页
Django使用MongoDB

Django使用MongoDB

作者: 有怪兽变身奥特曼 | 来源:发表于2022-05-07 08:44 被阅读0次
pip install django-rest-framework-mongoengine
pip install mongoengine

settings中设置

from mongoengine import connect
connect('admin', host=HOST, port=27017)
AUTHENTICATION_BACKENDS = ('mongoengine.django.auth.MongoEngineBackend',)

模型

import mongoengine
STATUS = ((0, '启用'), (1, '废弃'))
class Company(mongoengine.Document):
  name = mongoengine.StringField(max_length=100, required=True)
  fullname = mongoengine.StringField(max_length=250, required=True)
  address = mongoengine.StringField(max_length=250)
  status = mongoengine.IntField(default=0, choices=STATUS, required=False)
  date = mongoengine.DateTimeField()

序列化

from rest_framework_mongoengine import serializers
class CompanySerializer(serializers.DocumentSerializer):
  class Meta:
    model = Company
    fields = '__all__'

urls

router.register('company', CompanyView, base_name='company')

相关文章

网友评论

      本文标题:Django使用MongoDB

      本文链接:https://www.haomeiwen.com/subject/wefsertx.html