Foreplay
Django provides us with a back-end management system to help us manage the data table
Configuration
We default to log into the background does not show any one table, if you need to display, we need to be configured in the admin under the app we created
from django.contrib Import ADMIN from CRM Import Models # the Register your Models here Wallpaper. '' ' fixed wording, only later point in the table is the model ' '' admin.site.register (models.UserProfile) admin.site.register (models.Customer) admin.site.register (models.ClassList) admin.site.register (models.Campuses)
After this we configured to restart the project, log on to view in the background, Django will add a s shown on the page next to our table
If we want to customize the display, we can add a class Meta model in which
class the Customer (models.Model): "" " Customers table " "" QQ = models.CharField ( ' QQ ' , 64 = MAX_LENGTH, UNIQUE = True, help_text = ' number must be unique QQ ' ) # prompt help_textAdmin in the field unique information unique index qq_name = models.CharField ( ' QQ nickname ' , 64 = MAX_LENGTH, blank = True, null = True) # blank whether to allow the user to enter the Admin null null fields in the database can be empty if the name = models.CharField ( ' name ' , 32 = max_length, blank = true, null = true, help_text = ' after student enrollment, please change real name ') sex_type = (( ' MALE ' , ' male ' ), ( ' FEMALE ' , ' female ' )) Sex = models.CharField ( " sex " , choices = sex_type, max_length 16 =, default = ' MALE ' , blank = True, True = null) # default default database field Birthday = models.DateField ( ' date of birth ' , default = None, help_text = " format mm-dd-YYYY " , blank = True, null = True) Phone = Models.BigIntegerField('Phone number ' , blank = True, null = True) Source = models.CharField ( ' client base ' , 64-max_length =, = source_type choices, default = ' QQ ' ) introduce_from = models.ForeignKey ( ' Self ' , verbose_name = " referrals from students " , blank = True, null = True) # display field names in the verbose_name Admin course, = MultiSelectField ( " counseling courses " , choices = course_choices) class_type = models.CharField ( " class type ", 64 = MAX_LENGTH, choices = class_type_choices, default = ' fulltime ' ) customer_note = models.TextField ( " Customer Notes " , blank = True, null = True,) Status = models.CharField ( " state " , choices = enroll_status_choices, MAX_LENGTH 64 =, default = " Unregistered " , help_text = " select customer This state " ) network_consult_note = models.TextField (blank = True, null = True, the verbose_name = ' network consulting content consultants ') date Models.DateTimeField = ( " Date of consultation " , auto_now_add = True) last_consult_date = models.DateField ( " the last follow-up date " , auto_now_add = True) next_date = models.DateField ( " anticipated follow-up time again " , blank = True, null = True) network_consultant = models.ForeignKey ( ' UserProfile ' , blank = True, null = True, verbose_name = ' consultants ' , the related_name = ' network_consultant ' ) consultant = models.ForeignKey ( ' UserProfile ' , verbose_name = " sales " , the related_name = ' the Customers ' , blank = True, null = True,) # the related_name reverse lookup class_list = models.ManyToManyField ( ' classList ' , verbose_name = " has Daily classes " ,) class Meta: verbose_name = ' client list ' # If you just add the phrase, the latter s still displayed, you need to add the following sentence verbose_name_plural = ' customer list '
We have put together a few top class Meta, will be able to display properly
Use __str__ display specific content in the background
After logging back-office systems has been reported to add the class, the page displayed is an object, we need to be able to read into it, we need to add __str__ method
class ClassList(models.Model): """ 班级表 """ course = models.CharField("课程名称", max_length=64, choices=course_choices) semester = models.IntegerField("学期") campuses = models.ForeignKey('Campuses', verbose_name="校区") price = models.IntegerField("学费", default=10000) memo = models.CharField(''Description, Blank = True, null = True, MAX_LENGTH = 100 ) START_DATE = models.DateField ( " start dates " ) graduate_date = models.DateField ( " Completion date " , blank = True, null = True) Contract = models.ForeignKey ( ' ContractTemplate ' , the verbose_name = " select template contract " , blank = True, null = True) teachers = models.ManyToManyField ( ' the UserProfile ' , the verbose_name = " teacher " ) class_type= models.CharField(choices=class_type_choices, max_length=64, verbose_name='班额及类型', blank=True, null=True) def __str__(self): return '{}{}({})'.format(self.get_course_display(),self.semester,self.campuses)
Description:
Because our course is selected, the data source for choise = course_choices
= course_choices (( ' LinuxL ' , ' Linux-class ' ), # select courses ( ' PythonFullStack ' , ' Python senior full-stack development ' ),)
The results self.get_course for LinuxL or PythonFullStack, if used self.get_course_display () The result is that the latter value, Linux Python senior or senior full-stack development
Template language related
The query data in the database show up on the page, if a field in a data table is empty, then the page shows None, we can also use the filter to a default value
<td>{{ customer.qq_name|default:'暂无' }}</td>
If you are selected, we can also use get_ field names _display to obtain the corresponding value
<td>{{ customer.get_source_display }}</td>
Note: The template does not require (), and there is a need in the models.py () of
We can define a method in the model, the template used in
from django.utils.safestring import mark_safe
class the Customer (models.Model): "" " Customers table " "" QQ = models.CharField ( ' QQ ' , 64 = MAX_LENGTH, UNIQUE = True, help_text = ' number must be unique QQ ' ) # prompt help_textAdmin in the field unique information unique index defines a method, used to display different colors according to the state DEF show_status (Self): color_dict = { " Signed " : ' Green ' , " Unregistered " : ' Red ' , "studying": 'pink', "paid_in_full": 'blue', } return mark_safe( '<span style="background-color: {};color: white;padding: 4px">{}</span>'.format(color_dict[self.status], self.get_status_display()))
Template in use
< TD > {} {} customer.show_status show_status method call </ TD >
If you do not use mark_safe, can also be used in the template
<td> {{ customer.show_status|safe }} </td>
Because our data is last_consult_data field and use the following fields to define the
models.DateTimeField = DATE ( " Date of consultation " , auto_now_add = True) last_consult_date = models.DateField ( " the last follow-up date " , auto_now_add = True)
So in effect the following page display
Then we have two ways to display to the format we need first is to use the filter, the second increase in two lines of code and modify the line of code in settings.py
This time will be able to refresh the page to display properly
Because our Customer table and the table is many-ClassList
= models.ManyToManyField class_list ( ' classList ' , the verbose_name = " has been reported class " ,)
So if you want to query all the books we can write a method
def show_classes(self): return ' | '.join([str(i) for i in self.class_list.all()])
Use the template
<td>{{ customer.show_classes }}</td>