django操作mongodb数据库
一、构建项目及配置mongoengine
连接mongodb
数据库
二、在models
创建数据模型
- 1、注意这个地方要引入
import mongoengine
2、创建数据模型
class StudentModel(mongoengine.Document): """ 创建一个学生的数据模型 """ name = mongoengine.StringField(max_length=100, verbose_name='学生名字') age = mongoengine.IntField(verbose_name='学生年龄') gender = mongoengine.StringField(verbose_name='学生性别')
3、在视图中插入数据
class StudentView(View): def get(self, request, *args, **kwargs): return HttpResponse('hello word') def post(self, request, *args, **kwargs): name = request.POST.get('name', None) age = request.POST.get('age', None) gender = request.POST.get('gender', None) res = StudentModel.objects.create(name=name, age=age, gender=gender) if res: return HttpResponse('成功') return HttpResponse('失败')
4、
mongoengine
支持的字段- BinaryField
- BooleanField
- ComplexDateTimeField
- DateTimeField
- DecimalField
- DictField
- DynamicField
- EmailField
- EmbeddedDocumentField
- EmbeddedDocumentListField
- FileField
- FloatField
- GenericEmbeddedDocumentField
- GenericReferenceField
- GeoPointField
- ImageField
- IntField
- ListField
- MapField
- ObjectIdField
- ReferenceField
- SequenceField
- SortedListField
- StringField
- URLField
- UUIDField
- PointField
- LineStringField
- PolygonField
- MultiPointField
- MultiLineStringField
- MultiPolygonField
5、更多操作可以查看传送门