您现在的位置是: 网站首页 >Django >DjangoCRM客户关系管理 Django

【CRM客户关系管理】02.设置项目需要的模型

admin2018年11月23日 14:51 Django | Html | JavaScript | Python 458人已围观

DjangoCRM客户关系管理简介 使用Django2.1.3+Bootstrap实现CRM系统,仿照Django Admin重写后台 Github地址:https://github.com/xyliurui/DjangoCRM Django版本:2.1.3

# 设计模型 ![BLOG_20181123_145549_62](/media/blog/images/2018/11/BLOG_20181123_145549_62.png "博客图集BLOG_20181123_145549_62.png") ## 模型 创建系统需要用到的数据表 ```python from django.db import models from django.contrib.auth.models import User class Role(models.Model): """角色表""" name = models.CharField(max_length=50, unique=True, verbose_name='角色名称') class Meta: verbose_name_plural = verbose_name = '角色' def __str__(self): return self.name class UserProfile(models.Model): """用户信息表""" user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='useprofiles', verbose_name='关联系统User') # 扩展user模型 name = models.CharField(max_length=50, verbose_name='姓名') role = models.ManyToManyField(Role, related_name='userprofile', verbose_name='角色列表') class Meta: verbose_name_plural = verbose_name = '用户' def __str__(self): return self.name class Branch(models.Model): """校区分支""" name = models.CharField(max_length=50, unique=True, verbose_name='校区名') address = models.CharField(max_length=200, blank=True, null=True, verbose_name='地址') def __str__(self): return self.name class Meta: verbose_name_plural = verbose_name = '校区' class Course(models.Model): """课程表""" name = models.CharField(max_length=50, unique=True, verbose_name='课程名称') price = models.PositiveSmallIntegerField(verbose_name='价格') # 整数 period = models.PositiveSmallIntegerField(verbose_name='课程周期(月)', default=5) outline = models.TextField(verbose_name='大纲') def __str__(self): return self.name class Meta: verbose_name_plural = verbose_name = '课程' class ClassInFo(models.Model): """班级信息""" Class_Type_Choices = ( (1, '工作日'), (2, '周末'), (3, '网络班') ) branch = models.ForeignKey(Branch, on_delete=models.CASCADE, verbose_name='所属校区') course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name='课程') class_type = models.SmallIntegerField(choices=Class_Type_Choices, verbose_name='班级类型') semester = models.SmallIntegerField(verbose_name='学期') teachers = models.ManyToManyField(UserProfile, verbose_name='讲师') start_date = models.DateField(verbose_name='开班日期') graduate_date = models.DateField(blank=True, null=True, verbose_name='毕业日期') # 结束日期不固定,可为空 class Meta: verbose_name_plural = verbose_name = '班级信息' unique_together = ('branch', 'course', 'class_type', 'semester') # 联合唯一,班级不能重复 def __str__(self): return '{}({})期'.format(self.course.name, self.semester) class CourseRecord(models.Model): """上课记录""" class_grade = models.ForeignKey(ClassInfo, on_delete=models.CASCADE, verbose_name='班级') day_num = models.PositiveSmallIntegerField(verbose_name='课程节次') teacher = models.ForeignKey(UserProfile, on_delete=models.CASCADE, verbose_name='讲师') title = models.CharField(max_length=200, verbose_name='本节主题') content = models.TextField(verbose_name='本节内容') has_homework = models.BooleanField(default=False, verbose_name='本节是否有作业') homework = models.TextField(blank=True, null=True, verbose_name='作业内容') created_time = models.DateField(auto_now_add=True, verbose_name='创建时间') def __str__(self): return '{}第({})节'.format(self.class_grade, self.day_num) class Meta: verbose_name_plural = verbose_name = '上课记录' unique_together = ('class_grade', 'day_num') class CustomerInfo(models.Model): """客户信息表""" Contact_Type_Choices = ( (1, 'qq'), (2, '微信'), (3, '手机'), (4, '其他') ) Source_Choice = ( (1, 'qq群'), (2, '微信'), (3, '转介绍'), (4, '其它'), ) Status_Choice = ( (1, '未报名'), (2, '已报名'), (3, '结业') ) name = models.CharField(max_length=50, verbose_name='客户姓名') contact_type = models.SmallIntegerField(choices=Contact_Type_Choices, default=1, verbose_name='联系媒介') contact = models.CharField(max_length=50, unique=True, verbose_name='联系方式') source = models.SmallIntegerField(choices=Source_Choice, verbose_name='客户来源') # 如果是转介绍,介绍人是学员,介绍别人来学习,需要关联到学员本人,如果不是,可为空 referral_from = models.ForeignKey('self', blank=True, null=True, on_delete=models.SET_NULL, verbose_name='转介绍客户') consult_courses = models.ManyToManyField(Course, verbose_name='咨询课程') # 多对多关联课程 consult_content = models.TextField(verbose_name='咨询内容') status = models.SmallIntegerField(choices=Status_Choice, verbose_name='客户状态') consultant = models.ForeignKey(UserProfile, null=True, blank=True, on_delete=models.SET_NULL, verbose_name='课程顾问') created_time = models.DateField(auto_now_add=True, verbose_name='创建时间') def __str__(self): return self.name class Meta: verbose_name_plural = verbose_name = '客户信息' class CustomerFollowUp(models.Model): """客户跟进记录""" Status_Choices = ( (0, '近期无报名计划'), (1, '一个月内报名'), (2, '半个月报名'), (3, '已报名') ) customer = models.ForeignKey(CustomerInfo, on_delete=models.CASCADE, verbose_name='客户') content = models.TextField(verbose_name='跟进内容') user = models.ForeignKey(UserProfile, on_delete=models.CASCADE, verbose_name='跟进人') status = models.SmallIntegerField(choices=Status_Choices, verbose_name='客户状态') created_time = models.DateField(auto_now_add=True, verbose_name='创建时间') def __str__(self): return "{}跟进{}状态:{}".format(self.user.name, self.customer.name, self.get_status_display()) class Meta: verbose_name_plural = verbose_name = '跟进记录' class Student(models.Model): customer = models.OneToOneField(CustomerInfo, verbose_name='客户', on_delete=models.CASCADE) class_grades = models.ManyToManyField(ClassInfo, verbose_name='班级') class Meta: verbose_name_plural = verbose_name = '学员' def __str__(self): return '{}'.format(self.customer) class StudyRecord(models.Model): """学习记录""" Score_Choices = ( (100, 'A+'), (90, 'A'), (85, 'B+'), (80, 'B'), (75, 'B-'), (70, 'C+'), (60, 'C'), (40, 'C-'), (-50, 'D'), (0, 'N/A'), # not avaliable (-100, 'COPY'), # 抄作业 ) Show_Choices = ( (0, '缺勤'), (1, '已签到'), (2, '迟到'), (3, '早退'), ) course_record = models.ForeignKey(CourseRecord, on_delete=models.CASCADE, verbose_name='课程') student = models.ForeignKey(Student, verbose_name='学生', on_delete=models.CASCADE) score = models.SmallIntegerField(choices=Score_Choices, default=0, verbose_name='得分') show_status = models.SmallIntegerField(choices=Show_Choices, default=1, verbose_name='出勤') note = models.TextField(blank=True, null=True, verbose_name='成绩备注') created_time = models.DateField(auto_now_add=True, verbose_name='创建时间') class Meta: verbose_name_plural = verbose_name = '学习记录' def __str__(self): return '{} {} {}'.format(self.course_record, self.student, self.score) ``` ## 注册到admin 批量注册所有models ```python from django.contrib import admin from django.apps import apps # 批量注册models all_models = apps.get_app_config('crm').get_models() for model in all_models: try: admin.site.register(model) except: pass ``` ## 迁移同步数据库 ```bat manage.py@DjangoCRM > makemigrations manage.py@DjangoCRM > migrate ``` ## 创建admin ```bat manage.py@DjangoCRM > createsuperuser 用户名 (leave blank to use 'lr'): admin 电子邮件地址: admin@admin.com Warning: Password input may be echoed. Password: xxx Warning: Password input may be echoed. Password (again): xxx Superuser created successfully. ``` 然后运行服务器,访问 [管理地址](http://127.0.0.1:8000/admin/) 登录 ![BLOG_20181123_145520_82](/media/blog/images/2018/11/BLOG_20181123_145520_82.png "博客图集BLOG_20181123_145520_82.png")

很赞哦! (0)

文章交流

  • emoji
0人参与,0条评论

当前用户

未登录,点击   登录

站点信息

  • 建站时间:网站已运行683天
  • 系统信息:Linux
  • 后台程序:Python: 3.6.6
  • 网站框架:Django: 2.2.12
  • 文章统计:235 篇
  • 文章评论:40 条
  • 腾讯分析网站概况-腾讯分析
  • 百度统计网站概况-百度统计
  • 公众号:微信扫描二维码,关注我们
  • QQ群:QQ加群,下载网站的学习源码
返回
顶部
标题 换行 登录
网站