django 之admin后台管理

Posted 优雅随行ing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django 之admin后台管理相关的知识,希望对你有一定的参考价值。

数据库

  1 from django.db import models
  2 from django.contrib.auth.models import User
  3 from django.contrib.auth.models import (
  4     BaseUserManager, AbstractBaseUser,PermissionsMixin
  5 )
  6 from django.utils.translation import ugettext_lazy as _
  7 from django.utils.safestring import mark_safe
  8 
  9 # Create your models here.
 10 
 11 
 12 class Customer(models.Model):
 13     \'\'\'客户信息表\'\'\'
 14     name = models.CharField(max_length=32,blank=True,null=True,help_text="用户报名后请改为真实姓名")
 15     qq = models.CharField(max_length=64,unique=True)
 16     qq_name = models.CharField(max_length=64,blank=True,null=True)
 17     phone = models.CharField(max_length=64,blank=True,null=True)
 18     id_num = models.CharField(max_length=64,blank=True,null=True)
 19     email = models.EmailField(verbose_name="常用邮箱",blank=True,null=True)
 20     source_choices = ((0,\'转介绍\'),
 21                       (1,\'QQ群\'),
 22                       (2,\'官网\'),
 23                       (3,\'百度推广\'),
 24                       (4,\'51CTO\'),
 25                       (5,\'知乎\'),
 26                       (6,\'市场推广\')
 27                       )
 28 
 29     source = models.SmallIntegerField(choices=source_choices)
 30     referral_from = models.CharField(verbose_name="转介绍人qq",max_length=64,blank=True,null=True)
 31 
 32     consult_course = models.ForeignKey("Course",verbose_name="咨询课程")
 33     content = models.TextField(verbose_name="咨询详情")
 34     tags = models.ManyToManyField("Tag",blank=True,null=True)
 35     status_choices = ((0,\'已报名\'),
 36                       (1,\'未报名\'),
 37                       )
 38     status = models.SmallIntegerField(choices=status_choices,default=1)
 39     consultant = models.ForeignKey("UserProfile")
 40     memo = models.TextField(blank=True,null=True)
 41     date = models.DateTimeField(auto_now_add=True)
 42 
 43     def __str__(self):
 44         return "<%s %s>" %(self.qq,self.name)
 45 
 46     class Meta:
 47         verbose_name ="客户表"
 48         verbose_name_plural ="客户表"
 49 
 50 class Tag(models.Model):
 51     name = models.CharField(unique=True,max_length=32)
 52 
 53     def __str__(self):
 54         return self.name
 55 
 56     class Meta:
 57         verbose_name = "标签"
 58         verbose_name_plural = "标签"
 59 
 60 class CustomerFollowUp(models.Model):
 61     \'\'\'客户跟进表\'\'\'
 62     customer = models.ForeignKey("Customer")
 63     content = models.TextField(verbose_name="跟进内容")
 64     consultant = models.ForeignKey("UserProfile")
 65 
 66     intention_choices  = ((0,\'2周内报名\'),
 67                           (1,\'1个月内报名\'),
 68                           (2,\'近期无报名计划\'),
 69                           (3,\'已在其它机构报名\'),
 70                           (4,\'已报名\'),
 71                           (5,\'已拉黑\'),
 72                           )
 73     intention = models.SmallIntegerField(choices=intention_choices)
 74     date = models.DateTimeField(auto_now_add=True)
 75 
 76     def __str__(self):
 77         return "<%s : %s>" %(self.customer.qq,self.intention)
 78 
 79 
 80     class Meta:
 81         verbose_name = "客户跟进记录"
 82         verbose_name_plural = "客户跟进记录"
 83 
 84 class Course(models.Model):
 85     \'\'\'课程表\'\'\'
 86     name = models.CharField(max_length=64,unique=True)
 87     price = models.PositiveSmallIntegerField()
 88     period = models.PositiveSmallIntegerField(verbose_name="周期(月)")
 89     outline = models.TextField()
 90 
 91     def __str__(self):
 92         return self.name
 93 
 94     class Meta:
 95         verbose_name = "课程表"
 96         verbose_name_plural = "课程表"
 97 
 98 class Branch(models.Model):
 99     \'\'\'校区\'\'\'
100     name = models.CharField(max_length=128,unique=True)
101     addr = models.CharField(max_length=128)
102     def __str__(self):
103         return self.name
104 
105 
106     class Meta:
107         verbose_name = "校区"
108         verbose_name_plural = "校区"
109 
110 class ClassList(models.Model):
111     \'\'\'班级表\'\'\'
112     branch = models.ForeignKey("Branch",verbose_name="校区")
113     course = models.ForeignKey("Course")
114     class_type_choices = ((0,\'面授(脱产)\'),
115                           (1,\'面授(周末)\'),
116                           (2,\'网络班\')
117                           )
118     contract = models.ForeignKey("ContractTemplate",blank=True,null=True)
119 
120     class_type = models.SmallIntegerField(choices=class_type_choices,verbose_name="班级类型")
121     semester = models.PositiveSmallIntegerField(verbose_name="学期")
122     teachers = models.ManyToManyField("UserProfile")
123     start_date = models.DateField(verbose_name="开班日期")
124     end_date = models.DateField(verbose_name="结业日期",blank=True,null=True)
125 
126     def __str__(self):
127         return "%s %s %s" %(self.branch,self.course,self.semester)
128 
129     class Meta:
130         unique_together = (\'branch\',\'course\',\'semester\')
131         verbose_name_plural = "班级"
132         verbose_name = "班级"
133 
134 class CourseRecord(models.Model):
135     \'\'\'上课记录\'\'\'
136     from_class = models.ForeignKey("ClassList",verbose_name="班级")
137     day_num = models.PositiveSmallIntegerField(verbose_name="第几节(天)")
138     teacher = models.ForeignKey("UserProfile")
139     has_homework = models.BooleanField(default=True)
140     homework_title = models.CharField(max_length=128,blank=True,null=True)
141     homework_content = models.TextField(blank=True,null=True)
142     outline = models.TextField(verbose_name="本节课程大纲")
143     date = models.DateField(auto_now_add=True)
144 
145     def __str__(self):
146         return "%s %s" %(self.from_class,self.day_num)
147 
148     class Meta:
149         unique_together = ("from_class", "day_num")
150         verbose_name_plural = "上课记录"
151 
152 
153 class StudyRecord(models.Model):
154     \'\'\'学习记录\'\'\'
155     student = models.ForeignKey("Enrollment")
156     course_record = models.ForeignKey("CourseRecord")
157     attendance_choices = ((0,\'已签到\'),
158                           (1,\'迟到\'),
159                           (2,\'缺勤\'),
160                           (3,\'早退\'),
161                           )
162     attendance = models.SmallIntegerField(choices=attendance_choices,default=0)
163     score_choices = ((100,"A+"),
164                      (90,"A"),
165                      (85,"B+"),
166                      (80,"B"),
167                      (75,"B-"),
168                      (70,"C+"),
169                      (60,"C"),
170                      (40,"C-"),
171                      (-50,"D"),
172                      (-100,"COPY"),
173                      (0,"N/A"),
174                      )
175     score = models.SmallIntegerField(choices=score_choices,default=0)
176     memo = models.TextField(blank=True,null=True)
177     date = models.DateField(auto_now_add=True)
178 
179     def __str__(self):
180         return "%s %s %s" %(self.student,self.course_record,self.score)
181 
182     class Meta:
183         unique_together = (\'student\',\'course_record\')
184         verbose_name_plural = "学习记录"
185 
186 
187 class Enrollment(models.Model):
188     \'\'\'报名表\'\'\'
189     customer = models.ForeignKey("Customer")
190     enrolled_class = models.ForeignKey("ClassList",verbose_name="所报班级")
191     consultant = models.ForeignKey("UserProfile",verbose_name="课程顾问")
192     contract_agreed = models.BooleanField(default=False,verbose_name="学员已同意合同条款")
193     contract_approved = models.BooleanField(default=False,verbose_name="合同已审核")
194     date = models.DateTimeField(auto_now_add=True)
195 
196     def __str__(self):
197         return "%s %s" %(self.customer,self.enrolled_class)
198 
199     class Meta:
200         unique_together = ("customer","enrolled_class")
201         verbose_name_plural = "报名表"
202 
203 class Payment(models.Model):
204     \'\'\'缴费记录\'\'\'
205     customer = models.ForeignKey("Customer")
206     course = models.ForeignKey("Course",verbose_name="所报课程")
207     amount = models.PositiveIntegerField(verbose_name="数额",default=500)
208     consultant = models.ForeignKey("UserProfile")
209     date = models.DateTimeField(auto_now_add=True)
210 
211     def __str__(self):
212         return "%s %s" %(self.customer,self.amount)
213 
214     class Meta:
215         verbose_name_plural = "缴费记录"
216 
217 # class UserProfile(models.Model):
218 #     \'\'\'账号表\'\'\'
219 #     user = models.OneToOneField(User)
220 #     name = models.CharField(max_length=32)
221 #     roles = models.ManyToManyField("Role",blank=True,null=True)
222 #
223 #     def __str__(self):
224 #         return self.name
225 
226 
227 class ContractTemplate(models.Model):
228     \'\'\'合同模版\'\'\'
229     name = models.CharField("合同名称",max_length=64,unique=True)
230     template = models.TextField()
231 
232     def __str__(self):
233         return self.name
234 
235 
236 class UserProfileManager(BaseUserManager):
237     def create_user(self, email, name, password=None):
238         """
239         Creates and saves a User with the given email, date of
240         birth and password.
241         """
242         if not email:
243             raise ValueError(\'Users must have an email address\')
244 
245         user = self.model(
246             email=self.normalize_email(email),
247             name=name,
248         )
249 
250         user.set_password(password)
251         self.is_active = True
252         user.save(using=self._db)
253         return user
254 
255     def create_superuser(self,email, name, password):
256         """
257         Creates and saves a superuser with the given email, date of
258         birth and password.
259         """
260         user = self.create_user(
261             email,
262             password=password,
263             name=name,
264         )
265         user.is_active = True
266         user.is_admin = True
267         user.save(using=self._db)
268         return user
269 
270 
271 class UserProfile(AbstractBaseUser,PermissionsMixin):
272     \'\'\'账号表\'\'\'
273     email = models.EmailField(
274         verbose_name=\'email address\',
275         max_length=255,
276         unique=True,
277         null=True
278     )
279     password = models.CharField(_(\'password\'), max_length=128,
280                                 help_text=mark_safe(\'\'\'<a href=\'password/\'>修改密码</a>\'\'\'))
281     name = models.CharField(max_length=32)
282     is_active = models.BooleanField(default=True)
283     is_admin = models.BooleanField(default=False)
284     roles = models.ManyToManyField("Role",blank=True)
285     objects = UserProfileManager()
286 
287     stu_account = models.ForeignKey("Customer",verbose_name="关联学员账号",blank=True,null=True,
288                                     help_text="只有学员报名后方可为其创建账号")
289     USERNAME_FIELD = \'email\'
290     REQUIRED_FIELDS = [\'name\']
291 
292     def get_full_name(self):
293         # The user is identified by their email address
294         return self.email
295 
296     def get_short_name(self):
297         # The user is identified by their email address
298         return self.email
299 
300     def __str__(self):              # __unicode__ on Python 2
301         return self.email
302 
303     # def has_perm(self, perm, obj=None):
304     #     "Does the user have a specific permission?"
305     #     # Simplest possible answer: Yes, always
306     #     return True
307     #
308     # def has_module_perms(self, app_label):
309     #     "Does the user have permissions to view the app `app_label`?"
310     #     # Simplest possible answer: Yes, always
311     #     return True
312 
313     @property
314     def is_staff(self):
315         "Is the user a member of staff?"
316         # Simplest possible answer: All admins are staff
317         return self.is_active
318 
319 
320     class Meta:
321         permissions = ((\'can_fuck_him_to_death\',\'弄死小虎逼\'),
322                        (\'can_access_my_course\',\'可以访问我的课程\'),
323                        (\'can_access_customer_list\',\'可以访问客户列表\'),
324                        (\'can_access_customer_detail\',\'可以访问客户详细\'),
325                        (\'can_access_studyrecords\',\'可以访问学习记录页面\'),
326                        (\'can_access_homework_detail\',\'可以访问作业详情页面\'),
327                        (\'can_upload_homework\',\'可以交作业\'),
328                        (\'access_kingadmin_table_obj_detail\',\'可以访问kingadmin每个表的

以上是关于django 之admin后台管理的主要内容,如果未能解决你的问题,请参考以下文章

django 之admin后台管理

Django框架之admin管理后台

django之admin站点

django后台管理系统

Django之Admin组件

django 之admin模块