lslyq
lslyq
2021-05-12 17:57
采纳率: 100%
浏览 82

Django+MongoDB mysql改写

一个文档管理系统,本来数据库用的是mysql,但是现在需要改为mongodb数据库,对mongodb不熟悉,求求大佬们指导一下小弟都需要改写什么,我只在settings.py里面连接了数据库,具体在models.py里怎么改写。

settings.py

DATABASES = {
    'default': {
        'ENGINE': None,
        # 'ENGINE': 'django.db.backends.mysql',
        # 'NAME': 'test1',
        # 'USER': 'root',
        # 'PASSWORD': 'root',
        # 'HOST': '127.0.0.1',
    }
}
from mongoengine import connect
connect('test')

models.py(files)

from datetime import datetime
from mongoengine import *
from django.db import models


# 文件model
class File(models.Model):
    fileno = models.CharField(max_length=18, verbose_name='文件编号')
    filename = models.CharField(max_length=200, verbose_name='文件名称')
    filepath = models.CharField(max_length=200, verbose_name='文件路径')
    owner = models.CharField(max_length=30, verbose_name='文件所属用户')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='上传时间')
    first_check = models.CharField(max_length=1, choices=(('0', '未审批'), ('1', '已审批')), verbose_name='初审', default='0')
    second_check = models.CharField(max_length=1, choices=(('0', '未审批'), ('1', '已审批')), verbose_name='二审', default='0')
    isapprove = models.CharField(max_length=1, choices=(('0', '未审批'), ('1', '已审批'), ('2', '已驳回')), verbose_name='终审', default='0')

    class Meta:
        verbose_name = '文件'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.filename


# 文件审批日志model
class ApproveLog(models.Model):
    fileno = models.CharField(max_length=18, verbose_name='文件编号')
    filename = models.CharField(max_length=200, verbose_name='文件名称')
    owner = models.CharField(max_length=30, verbose_name='文件所属用户')
    add_time = models.DateTimeField(verbose_name='上传时间')
    isapprove = models.CharField(max_length=1, choices=(('1', '已审批'), ('2', '已驳回')), verbose_name='终审')
    approve_time = models.DateTimeField(default=datetime.now, verbose_name='审批时间')

    class Meta:
        verbose_name = '文件审批日志'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.filename


# 公用电脑ip地址model
class PublicIp(models.Model):
    host_ip = models.CharField(max_length=300, verbose_name='公用电脑ip地址', blank=True)

    class Meta:
        verbose_name = '公用电脑ip地址'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.host_ip

models.py(users)

from datetime import datetime

from django.db import models
from django.contrib.auth.models import AbstractUser

# 定义用户模型,添加额外的字段
class UserProfile(AbstractUser):
    userno = models.CharField(max_length=15, verbose_name='工号')
    department = models.CharField(max_length=15, verbose_name='部门', blank=True)
    role = models.CharField(max_length=1, choices=(('3', '监察稽核员'), ('2', '系统管理员'),
                                                   ('1', '普通员工'), ('0', '离职员工')),
                            verbose_name='用户角色', default='1')
    sub_role = models.CharField(max_length=1, choices=(('2', '二审员'), ('1', '初审员'), ('0', '')),
                                verbose_name='子角色', default='0')
    is_superuser = models.IntegerField(verbose_name='超级管理员', default=0)
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = '用户信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username


# 定义用户操作日志模型
class UserOperateLog(models.Model):
    userno = models.CharField(max_length=15, verbose_name='工号')
    username = models.CharField(max_length=20, verbose_name='姓名')
    type = models.CharField(max_length=20, verbose_name='操作类型')
    comment = models.CharField(max_length=20, verbose_name='备注')
    fileno = models.CharField(max_length=18, verbose_name='文件编号')
    filename = models.CharField(max_length=200, verbose_name='文件名称')
    modify_time = models.DateTimeField(default=datetime.now, verbose_name='操作时间')

    class Meta:
        verbose_name = '用户操作日志'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username + '.' + self.type
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • technologist_24
    CSDN专家-黄老师 2021-05-12 18:48
    已采纳

    参考一下:https://blog.csdn.net/Coxhuang/article/details/89555660

    我这边测试是没问题的

    点赞 评论
  • lslyq
    lslyq 2021-05-13 14:59

    黄老师,我按照这个修改还是连接不上mongodb,麻烦您再看一下,感恩。

     

    modles.py(files)

    from datetime import datetime
    from mongoengine import *
    from django.db import models
    
    
    # 文件model
    class File(Document):
        fileno = StringField(max_length=18, verbose_name='文件编号')
        filename = StringField(max_length=200, verbose_name='文件名称')
        filepath = StringField(max_length=200, verbose_name='文件路径')
        owner = StringField(max_length=30, verbose_name='文件所属用户')
        add_time = DateTimeField(default=datetime.now, verbose_name='上传时间')
        first_check = StringField(max_length=1, choices=(('0', '未审批'), ('1', '已审批')), verbose_name='初审', default='0')
        second_check = StringField(max_length=1, choices=(('0', '未审批'), ('1', '已审批')), verbose_name='二审', default='0')
        isapprove = StringField(max_length=1, choices=(('0', '未审批'), ('1', '已审批'), ('2', '已驳回')), verbose_name='终审', default='0')
    
        class Meta:
            verbose_name = '文件'
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.filename
    
    
    # 文件审批日志model
    class ApproveLog(Document):
        fileno = StringField(max_length=18, verbose_name='文件编号')
        filename = StringField(max_length=200, verbose_name='文件名称')
        owner = StringField(max_length=30, verbose_name='文件所属用户')
        add_time = DateTimeField(verbose_name='上传时间')
        isapprove = StringField(max_length=1, choices=(('1', '已审批'), ('2', '已驳回')), verbose_name='终审')
        approve_time = DateTimeField(default=datetime.now, verbose_name='审批时间')
    
        class Meta:
            verbose_name = '文件审批日志'
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.filename
    
    
    # 公用电脑ip地址model
    class PublicIp(Document):
        host_ip = StringField(max_length=300, verbose_name='公用电脑ip地址', blank=True)
    
        class Meta:
            verbose_name = '公用电脑ip地址'
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.host_ip
    

    modles.py(users)

    from datetime import datetime
    from mongoengine import *
    from django.db import models
    from django.contrib.auth.models import AbstractUser
    
    # 定义用户模型,添加额外的字段
    class UserProfile(AbstractUser):
        userno = models.CharField(max_length=15, verbose_name='工号')
        department = models.CharField(max_length=15, verbose_name='部门', blank=True)
        role = models.CharField(max_length=1, choices=(('3', '监察稽核员'), ('2', '系统管理员'),
                                                       ('1', '普通员工'), ('0', '离职员工')),
                                verbose_name='用户角色', default='1')
        sub_role = models.CharField(max_length=1, choices=(('2', '二审员'), ('1', '初审员'), ('0', '')),
                                    verbose_name='子角色', default='0')
        is_superuser = models.IntegerField(verbose_name='超级管理员', default=0)
        add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')
    
        class Meta:
            verbose_name = '用户信息'
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.username
    
    
    # 定义用户操作日志模型
    class UserOperateLog(Document):
        userno = StringField(max_length=15, verbose_name='工号')
        username = StringField(max_length=20, verbose_name='姓名')
        type = StringField(max_length=20, verbose_name='操作类型')
        comment = StringField(max_length=20, verbose_name='备注')
        fileno = StringField(max_length=18, verbose_name='文件编号')
        filename = StringField(max_length=200, verbose_name='文件名称')
        modify_time = DateTimeField(default=datetime.now, verbose_name='操作时间')
    
        class Meta:
            verbose_name = '用户操作日志'
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.username + '.' + self.type
    
    点赞 评论
  • QA_Assistant
    有问必答小助手 2021-05-13 16:11

    您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

    如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

    ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

    点赞 评论

相关推荐