Django model definition

Source: Internet
Author: User

The time when the INF math data of a decimal student is modified is the time when the boolean type is created.

  • Define Model: Inmodels.pyIt is defined in the file. A data table is a model. If the database has three tables: Student, info, and grade, the meaning of the three tables is student table, student information, and class information, the three tables are associated, the students and classes are in one-to-many relationship, and the student-to-student information is in one-to-one relationship. The model is defined as follows.

  • Define the Student Model

Class grage (models. model): g_name = models. charfield (max_length = 10, unique = true) # The maximum length is 10, and the class name is unique class meta: db_table = 'grad' # specify the table name as grade. If this parameter is not specified, the default value is application name_grade.
  • Define the one-to-many relationship between the student model and the class
Class student (models. model): s_id = models. integerfield (unique = true) # defines the student ID, and the unique s_name = models. charfield (max_length = 20) # define the Student name. The maximum length is 20 s_gender = models. booleanfield (default = 1) # defines the Student gender and boolean type. The default value is 1. True s_age = models. integerfield () # define the student age create_time = models. datetimefield (auto_now_add = true, null = true) # defines the creation time. auto_now_add indicates that it is created at the current time and will not be changed. null = true indicates that it is null by default. If no error is reported, update_time = models. datetimefield (auto_now = true, null = true) # defines the modification time. auto_now indicates each modification. The value in the field is when the modification time is Chinese = models. decimalfield (decimal_places = 1, max_digits = 4, null = true) # define the Chinese score. decimal_places indicates one decimal place, And max_digits = 4 indicates a maximum of four valid places, such as 123.5, allow null math = models. decimalfield (decimal_places = 1, max_digits = 4, null = true) # define the mathematical score grade = models. foreginkey (grade, null = true) # defines foreign key constraints. One Class corresponds to multiple students info = models. onetoonefield (Info, null = true) # defines the 1-to-1 relationship. The student and Student Information Class meta: db_table = 'student '# specifies the table name as student.
  • Define the relationship between info model and student 1-to-1
Class Info (models. model): Phone = models. charfield (max_length = 10, null = true) # defines the mobile phone number. The maximum value is 11 characters. Address = models. charfield (max_length = 50, null = true) # defines the address. The maximum value is 50 class meta: db_table = 'info' # specifies the table name.

Django model definition

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.