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')