Design idea of python2.0_day19_ background database

Source: Internet
Author: User
Tags readable

From django.db import Models

# Create your models here.

From Django.contrib.auth.models import User

Course_type_choices = ((' Online ', U ' Network class '),
(' Offline_weekend ', U ' Class (Weekend) '),
(' Offline_fulltime ', U ' face-to-face class (out of the Classroom) '),
)


# Campus Table
Class School (models. Model):
Name = models. Charfield (max_length=128,unique=true) # Unique=true function is to return to the front end or call when not an object, but a specific data, can distinguish who he is
City = models. Charfield (max_length=64)
Addr = models. Charfield (max_length=128)

def __str__ (self):
Return Self.name

# Basic User Table
Class UserProfile (models. Model):
# user = models. ForeignKey (user) # We want to use Django's own user for authentication, but not ForeignKey (), because an internal account cannot have multiple users
user = models. Onetoonefield (user,verbose_name=u "associated user")
# using the Onetoonefield () in Django models to restrict an internal account to one user,
# just implemented in the Django Admin Level 1 pairs of 1,mysql itself is not implemented with 1 to 1 foreign keys.
# bottom-ForeignKey ()
Name = models. Charfield (U "name", max_length=64) # u "Name" This is the field name that is displayed in admin background management can also use Verbose_name=u ' name ', the difference is that verbose_name can be used in any field, The U "name" cannot be used for special fields such as ForeignKey (), user name, maximum length 64.
School = models. ForeignKey (School) # A user must belong to at least one center
def __str__ (self):
Return "%s (%s)"% (Self.name,self.school)
Class Meta:
Verbose_name = u "User" # The singular situation shows that the meaning of Verbose_name is simple, which is to give your model class A more readable name.
verbose_name_plural = u "user" # The plural situation shows that if you do not specify Django will automatically add a ' s ' after the model name, such as the default display as user profiles


# define Curriculum
Class Course (models. Model):
Name = models. Charfield (Max_length=64,unique=true)
Online_price = models. Integerfield ()
Offline_price = models. Integerfield ()
Inroduction = models. TextField () #课程介绍字段

def __str__ (self):
Return Self.name

# Write a class table
Class Classlist (models. Model):
Course = models. ForeignKey (course,verbose_name=u "course")
Semester = models. Integerfield (verbose_name=u "semester")

Teachers = models. Manytomanyfield (UserProfile)
Start_date = models. Datefield ()
Graudate_date = models. Datefield ()
# when creating a class, you want to divide the network version and the class (for the weekend)
# when we created the customer information sheet, we chose the type of course we consulted. This course type is also used here. So I'm going to write it again. But what if I don't want to write?
# If you don't want to write, put the course_type_choices in the Customer information table and put it outside the class so that it is a global variable that can be used by multiple classes
Course_type = models. Charfield (max_length=64,choices=course_type_choices)

def __str__ (self):
Return '%s ' (%s) '% (Self.course.name,self.course_type,self.semester)

Class Meta:
Unique_together = (' Course ', ' semester ', ' Course_type ')

# Customer table, store customer consultation information
Class Customer (models. Model):
QQ = models. Charfield (Max_length=64,unique=true)
Name = models. Charfield (Max_length=32,blank=true,null=true)
# phone = models. Integerfield () #不能用IntegerField, not longer than 10 bits long
Phone = models. Bigintegerfield (blank=true,null=true) #blank =true allow null at the admin level, NULL at the database level
Course = models. ForeignKey (Course)
# 1. More types of courses (network and face-to-face in each course) 2. Update course information dynamically in view of these two conditions, the course record should be made a separate table
# course_type_choices = ((' Online ', U ' Network class '),
# (' Offline_weekend ', U ' Class (Weekend) ')
# (' Offline_fulltime ', U ' face-to-face class (out of the classroom) ')
# )
Course_type = models. Charfield (max_length=64,choices=course_type_choices,default= ' offline_weekend ') # defines the consulting type field, choices here is just the Django The admin level becomes optional
# The default selection box is not returned for the Django API
Consult_memo = models. TextField ()
# Make a variable, like Next, source select list
Source_type_choices = (' QQ ', U ' QQ Group '),
(' Referral ', U "Internal introduction"),
(' 51cto ', U "51cto"),
(' Agent ', U ' Admissions Agent '),
(' Others ', U ' other ')
)
Source_type = models. Charfield (choices=source_type_choices,max_length=128,default= ' QQ ')
# students to introduce, create a field, associate a customer in this table, a knowledge point to associate themselves, the focus is ForeignKey (' self '), is the quote self, because he has not been created, or write his own table name ForeignKey (' Customer ')
Referral_from = models. ForeignKey (' self ', blank=true,null=true,related_name= ' referral_who ')
Status_choieses = ((' Signed ', U "registered"),
(' Unregistered ', U "not enrolled"),
(' Graduated ', U "graduated"),
(' Drop-off ', U "withdrawal")
)
Status = Models. Charfield (max_length=64,choices=status_choieses)
Consultant = models. ForeignKey (' UserProfile ', verbose_name=u "course advisor") # Verbose_name's role is to display Chinese in the form class or admin background
Class_list = models. Manytomanyfield (' classlist ', blank=true) # If you register, you can associate the class table, many to many
Date = models. Datefield (U "consultation Date", auto_now_add=true) #默认填入当前日期

def __str__ (self):
Return "%s (%s)"% (Self.qq,self.name)






# Customer Tracking Record
# First of all we're pretty much logged to the Customer table summary. But a customer may have multiple consulting records, and we have only one field in the user table that records the consultation record. We must have more than one record, so we're going to create a table specifically to store the consulting records.
Class Customertrackrecord (models. Model):
Customer = models. ForeignKey (Customer) #关联客户
Track_record = models. TextField (U "track record")
Track_date = models. Datefield ()
Follower = models. ForeignKey (userprofile) #跟踪人
Status_choices = ((1,u "No recent Registration Plan"),
(2,u "Registration within 2 months"),
(3,u "Registration within 1 months"),
(4,u "Registration within 2 weeks"),
(5,u "Registration within 1 weeks"),
(6,u "Registration within 2 days"),
(7,u "registered"),
)
Status = Models. Integerfield (choices=status_choices,help_text=u "Select customer status at this time") # Inquiry result Status
def __str__ (self):
Return Self.customer

# Class attendance record form
# First we create a client-to-class association in the Customer table, and each class can reverse-find fewer students
# But each class has a lot of days of courses, is not should create a schedule, the table to the class related. Then associate each student in the class with a record of the student's daily learning situation.
Class Courserecord (models. Model):
Class_obj = models. ForeignKey (classlist)
Day_num = models. Integerfield (U "First Class") #显示的时候 "lesson of the day"
Course_date = models. Datefield (Auto_now_add=true,verbose_name=u "Class Time")
Teacher = models. ForeignKey (userprofile) # Lecturer
# students = models. Manytomanyfield (Customer) # This field reflects the relationship between the learner and the class record in a Manytomany way. Then the question comes, and we know that a relationship table is created after the manytomany is defined.
# So how do we show that the students are late? How do you make sure who is not coming when you create it? You can say that you create a record after a roll call, but you can show that you are late. Of course not.
# The ideal way to do this is not to associate students in a manytomany way, but to create a learning record table. The learning record can be associated with the class record table for each learner, so this is commented out
def __str__ (self):
Return "%s,%s"% (self.class_obj,self.day_num)

Class Meta:
Unique_together = (' Class_obj ', ' Day_num ') # The same class cannot appear on the same day course 2 times


# Student's learning record form, sign-in status, score
Class Studyrecord (models. Model):
Student = models. ForeignKey (Customer)
Record_choices = (' checked ', U "checked in"),
(' Late ', U "late"),
(' NoShow ', U "absence"),
(' Leave_early ', U "Leave early"),
)
Record = models. Charfield (U "state", choices = record_choices,max_length=64)
#此处需要注意, when we display records in an HTML template file or in admin background, the record is displayed in English instead of Chinese in the option.
# If you want to display Chinese in the HTML template file you need to write Obj.get_record_display, and in the admin to define the table admin, Get_record_display
#关联班级上课记录表
Course_record = models. ForeignKey (Courserecord)
Score_choices = ((+, ' A + '),
("A"),
(the ' B + '),
("B"),
(+, ' B '),
(The ' c+ '),
(' C '),
(+, ' C '),
(0, ' D '),
( -1, ' n/a '),
( -100, ' COPY '),
( -1000, ' FAIL '),
)
Score = models. Integerfield (U "score in this section", Choices=score_choices,default=-1)
Date = models. Datefield ()
Note = models. Charfield (U "remarks", Max_length=255,blank=true,null=true)

def __str__ (self):
Return "%s,%s,%s,%s"% (Self.course_record,self.student,self.record)


# Summary of admin background display issues:
# 1. For the table itself in the background management interface, each table is the English table name, if you want to display in Chinese, you need to define the following in the table class:
# class Meta:
# verbose_name = u "User" # The singular situation shows that the meaning of verbose_name is simply to give your model class A more readable name.
# verbose_name_plural = u "user" # plural situation shows that if you do not specify Django will automatically add a ' s ' after the model name, such as the default display as user profiles

# 2.1 Records in the default table show the object. You can define
# def __str__ (self):
# return "%s,%s"% (Self.name,self.school)
# The format to display when only one field is displayed

# 2.2 The records in the table show the objects by default, and if you want to define the display of multiple fields, you can define which fields are displayed in the admins.py file, such as:
# class Bookadmin (admin. Modeladmin):
# List_display = (' title ', ' publisher ', ' Publication_date ') #指定显示的字段
#
# Admin.site.register (models. Author)
# Admin.site.register (models. Book,bookadmin) # When registering, pass in the defined Bookadmin class as a parameter
# Admin.site.register (models. Publisher)
# 3. The display of field names in the table is the field name by default and can be displayed as Chinese by verbose_name= "Chinese name"

Design idea of python2.0_day19_ background database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.