• 2.Model设计


    Model设计

    1.在settings.py中配置:

    AUTH_USER_MODEL='users.UserProfile'

     2.设计model

    1.在apps/users/models.py中:

    from django.db import models
    from django.contrib.auth.models import AbstractUser
    from datetime import datetime
    from django.utils.safestring import mark_safe
    # Create your models here.
    
    
    class UserProfile(AbstractUser):
        """
        用户表
        """
        token = models.CharField(max_length=64, null=True, blank=True, verbose_name='token')
        name = models.CharField(max_length=10, null=True, blank=True, verbose_name='姓名')
        minzu = models.CharField(max_length=10, null=True, blank=True, verbose_name='民族')
        sex = models.IntegerField(choices=((1, ''), (2, ''), (3, '不详')), default=3, verbose_name='性别',
                                  help_text='1:男,2:女,3:不详')
        mobile = models.CharField(max_length=11, null=True, blank=True, verbose_name='电话')
        address=models.TextField(max_length=100,null=True, blank=True,verbose_name='现居地址',help_text='最多100字')
        danwei = models.TextField(max_length=100, null=True, blank=True, verbose_name='工作单位', help_text='最多100字')
        zhiwei=models.CharField(max_length=10,null=True, blank=True,verbose_name='职位',help_text='最多10字')
        is_dangyuan=models.BooleanField(default=False,verbose_name='是否是党员')
        hujixz = models.IntegerField(choices=((1, '农业户口'), (2, '非农业户口')), verbose_name='户籍性质', default=2,
                                     help_text='1:农业户口,2:非农业户口')
        huji = models.TextField(max_length=100, null=True, blank=True, verbose_name='户籍地址', help_text='最多100字')
        wenhua=models.IntegerField(choices=((0, ''), (1, '小学'), (2, '初中'),(3,'高中'),(4,'中专'),(5,'大专'),(6,'本科'),
                                            (7,'硕士'),(8,'博士')), default=0, verbose_name='学历',
                                   help_text='0:无,1:小学,2:初中,3:高中,4:中专,5:大专,6:本科,7:硕士,8:博士')
        hunyin=models.IntegerField(choices=((1, '未婚'), (2, '已婚'), (3, '离异'),(4,'丧偶')), default=2, verbose_name='婚姻状况',
                                  help_text='1:未婚,2:已婚,3:离异,4:丧偶')
        no = models.CharField(max_length=20, null=True, blank=True, verbose_name='身份证号')
        chusheng = models.DateField(null=True, blank=True, verbose_name="出生日期")
        is_teshu=models.BooleanField(default=False,verbose_name='是否特殊群体')
        teshu = models.CharField(max_length=10,null=True, blank=True,verbose_name='特殊群体',help_text='最多10字')
        beizhu=models.TextField(max_length=200,null=True,blank=True,verbose_name='备注',help_text='最多200字')
        power = models.IntegerField(choices=((1, '普通用户'), (2, '单元长(组长)'), (3, '楼长(街长)'), (4, '网格员'), (5, '网格长')), default=1,
                                    verbose_name='用户权限',
                                    help_text='1:普通用户,2:单元长(组长),3:楼长(街长),4:网格员,5:网格长')
        img = models.ImageField(upload_to='img',verbose_name='照片',help_text='只有网格长和网格员需要',null=True, blank=True,)
        work=models.TextField(max_length=100,null=True,blank=True,verbose_name='工作范围',help_text='只有网格长和网格员需要(最多100字)')
    
        class Meta:
            verbose_name='用户'
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.username
    
    
    class PianQu(models.Model):
        """
        片区表
        """
        banner = models.ImageField(upload_to='banner',null=True,blank=True,verbose_name='轮播图')
        name = models.CharField(max_length=20, verbose_name='片区名',help_text='最长20字')
        wgz=models.ForeignKey(UserProfile,verbose_name='网格长',on_delete=models.PROTECT,help_text='不可空')
        add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")
    
        class Meta:
            verbose_name = "片区表"
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.name
    
        def image_url(self):
            return mark_safe('<img src="/media/{0}" class="field_img">'.format(self.banner))
    
        image_url.short_description = '轮播图'
    
    
    class LouMing(models.Model):
        """
        楼名(街名)
        """
        name = models.CharField(max_length=10, verbose_name='楼名(街名)', help_text='最长10字')
        pianqu=models.ForeignKey(PianQu,verbose_name='所属片区',on_delete=models.PROTECT)
        lz=models.ForeignKey(UserProfile,verbose_name='楼长',on_delete=models.PROTECT,null=True,blank=True,related_name='lz')
        wgy=models.ForeignKey(UserProfile,verbose_name='网格员',on_delete=models.PROTECT,help_text='不可空')
        add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")
    
        class Meta:
            verbose_name = "楼名(街名)表"
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.name
    
    
    class DanYuan(models.Model):
        """
        单元名(组名)
        """
        name = models.CharField(max_length=10, verbose_name='单元名(组名)', help_text='最长10字')
        lou=models.ForeignKey(LouMing,verbose_name='所属楼(街)',on_delete=models.PROTECT)
        dyz = models.ForeignKey(UserProfile, verbose_name='单元长(组长)', on_delete=models.PROTECT,blank=True,null=True,help_text='可空')
        add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")
    
        class Meta:
            verbose_name = "单元名(组名)表"
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.name
    
    
    class ZhuHu(models.Model):
        """
        住户表
        """
        name = models.CharField(max_length=10, verbose_name='门牌号', help_text='最长10字')
        dy=models.ForeignKey(DanYuan,verbose_name='所属单元(组)',on_delete=models.PROTECT)
        hz = models.ForeignKey(UserProfile, verbose_name='户主', on_delete=models.PROTECT, blank=True, null=True,
                                help_text='可空')
        add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")
    
        class Meta:
            verbose_name = "门牌号表"
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.name
    
    
    class UserToZhuHu(models.Model):
        """
        用户与门牌号的对应表
        """
        user=models.ForeignKey(UserProfile,verbose_name='用户',on_delete=models.CASCADE)
        zhhu=models.ForeignKey(ZhuHu,verbose_name='门牌号',on_delete=models.CASCADE)
        add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")
    
        class Meta:
            verbose_name = "用户To门牌号表"
            verbose_name_plural = verbose_name

    2.在apps/user_operations/models.py中:

    from django.db import models
    from users.models import UserProfile
    from datetime import datetime
    # Create your models here.
    
    
    class Notice(models.Model):
        """公告"""
    
        user=models.ForeignKey(UserProfile,verbose_name='发布者',on_delete=models.PROTECT,null=True,blank=True)
        content=models.CharField(max_length=100,verbose_name='公告内容')
        add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")
    
        class Meta:
            verbose_name = "公告表"
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.content

    3.打开MySQL数据库终端:

    show databases;
    drop database newcenter;
    create database NewCenter default character set utf8 collate utf8_general_ci; --创建数据库NewCenter,因为创建数据表内有中文字段,所以要加default
    show databases; --查看所有数据库

    4.执行数据更新命令:

    python manage.py makemigrations
    python manage.py migrate
  • 相关阅读:
    the selected directory is not a TomEE home
    java 获取当前年份 月份,当月第一天和最后一天
    java jwt工具类,生成和解析token
    Thymeleaf嵌套循环,每次循环显示固定数量
    oracle 非正常关机 错误1033解决办法
    webService简单记录
    好用的json工具
    org.apache.ws.commons.schema.XmlSchemaForm.getValue()Ljava/lang/String;
    webservice获取ip地址
    JSP(一)
  • 原文地址:https://www.cnblogs.com/xuepangzi/p/13058353.html
Copyright © 2020-2023  润新知