master
BINGWU 8 months ago
parent 21ab061a3f
commit bedff6d160

@ -1,6 +1,12 @@
import mongoengine from mongoengine import Document, EmbeddedDocument, fields
class UserModel(mongoengine.Document): class SubjectModel(EmbeddedDocument):
username = mongoengine.StringField(max_length=30) subjectName = fields.StringField()
password = mongoengine.StringField(max_length=30) subjectScore = fields.IntField()
class UserModel(Document):
username = fields.StringField(max_length=30)
password = fields.StringField(max_length=30)
subjects = fields.ListField(fields.EmbeddedDocumentField(SubjectModel))

@ -0,0 +1,9 @@
from pymongo import MongoClient
def connectdb(collectionName):
# 连接到 MongoDB 数据库
client = MongoClient('mongodb://localhost:27017/')
db = client['employee-information-management-system']
collection = db[collectionName]
return collection

@ -0,0 +1,17 @@
from pymongo import MongoClient
from bson.objectid import ObjectId
def test():
# 连接到 MongoDB 数据库
client = MongoClient('mongodb://localhost:27017/')
db = client['employee-information-management-system']
collection = db['user_model']
# 获取文档的 _id 字段的值
document_id = '660195d50d5cb377410134f7' # 替换为你要查找的文档的 _id
document = collection.find_one({'_id': ObjectId(document_id)})
if document:
document_id = document['_id']
print(document)
else:
print("Document not found.")

@ -1,17 +1,49 @@
from django.shortcuts import render, HttpResponse from django.shortcuts import render, HttpResponse
from serve.models.user import UserModel from serve.models.user import UserModel
from django.http import JsonResponse
import json import json
from django.urls import path from django.urls import path
from serve.utils.connectdb import connectdb
from bson.objectid import ObjectId
collection = connectdb('user_model')
def createUser(request): def createUser(request):
print(request.method) print(request.method)
data = json.loads(request.body) data = json.loads(request.body)
print(data.get('name')) print(data.get('name'))
UserModel.objects.create(username='154782456', password='123456') UserModel.objects.create(username='154782456', password='123456', subjects=[
{
'subjectName': "语文",
'subjectScore': 90
}]
)
return HttpResponse("插入成功") return HttpResponse("插入成功")
def deleteUser(request):
result = UserModel.objects.filter(id="ly").first().delete()
print(result)
return HttpResponse('删除成功')
def updateUser(request):
result = UserModel.objects.filter(id='1').first().update(username="ly")
return HttpResponse(result)
def getUser(request):
# user = UserModel.objects.get(id='660195d50d5cb377410134f7')
user = collection.find_one({'_id': ObjectId('660195d50d5cb377410134f7')})
user['_id'] = str(user['_id'])
# 返回 JSON 响应
return JsonResponse(user)
user_url = [ user_url = [
path('user/create', createUser), path('user/create', createUser),
path('user/delete', deleteUser),
path('user/update', updateUser),
path('user/get', getUser),
] ]

Loading…
Cancel
Save