Django learning---Day2-The construction of a Django complete project

Django learning day 2 (01)


content

On the basis of the first three summaries, a stu application app is recreated here, and the models module is analyzed and the code is organized

  • Creation of stu project
  • Create stu models in models
  • data migration
  • perform migration
  • Create a super administrator account
  • Two methods of registration and modification of page content
  • Django model field reference

Creation of stu project

For the detailed method, see the creation of myapp in the previous article. It is exactly the same as myapp, but it is a different application, side by side with each other, with different names.

Create stu models in models

Define the class model name in the models.py file
Inheriting models.Model
is equivalent to creating a class under the models module and defining different methods under the class

from django.db import models

### 定义一个Student类
class Student(models.Model):
    # 数据的保持 有姓名和性别属性
    s_name =  models.CharField(max_length=20)
    s_gender = models.BooleanField()
    # db_tables:定义数据库中的表名称,如果在这里不这么定义,就会在数据库中显示为db_table_Student
    class Meta:
        db_table = 'stu'
# stu的创建这里的属性字段类型有:
CharField()
BooleanField()

database migration

1. The name of the database has been defined in the previous settings
2. Principle: Generate database files
3. Generate migration files
Command : python manage.py makemigrations


Perform database migration

1. On the basis of the previous step, execute the following command to migrate files.
2. Principle: Migrate data to the database
3. Complete the migration task
Command : python manage.py migrate

Create a super administrator account

1. To add students in the management background (admin), you need to create an administrator account-password
command : python manage.py createsuperuser
and then set the account, email, password, and repeat the verification password (the password cannot be too simple)

python manage.py createsuperuser
Username: wangmomo
Email address:momow26@163.com
Password:wangmomo

2. View in the MySQL database, you can see the account and password
auth_user column in the background, you can see the information
3. Log in to the background, you will see the newly created User and admin accounts
Users are the users just created
4. In Under the admin.py file under the newly created stu project file, register the model, and in the background, you can add, delete, check and modify the defined stu model.

# 需要先导入自己在models中创建的学生Student类
from django.contrib import admin
from stu.models import Student

# Register your models here.
admin.site.register(Student)

Two methods of registration and modification of page content

1. Define a class in admin.py and display the list of students

from django.contrib import admin
from stu.models import Student


# Register your models here.
class StudentAdmin(admin.ModelAdmin):
    # 列表,只展示id和name字段,同时把StudentAdmin注册进去
    list_display = ['id', 's_name', 's_gender']

admin.site.register(Student, StudentAdmin)

2. Here you need to define a method to change the gender from the default 1 and 0 to "male" and "female"
and define in class StudentAdmin(admin.ModelAdmin):

    def set_s_gender(self):
        if self.s_gender:
            return '男'
        else:
            return '女'

3. Some page decoration commands

from django.contrib import admin
from stu.models import Student

# Register your models here.
class StudentAdmin(admin.ModelAdmin):
    # 设置性别为男女显示
    def set_s_gender(self):
        if self.s_gender:
            return '男'
        else:
            return '女'
    # 修改性别字段的描述
    set_s_gender.short_description = '性别'
    # 展示字段列表,只展示id和name字段,同时把StudentAdmin注册进去
    list_display = ['id', 's_name', set_s_gender]
    # 过滤,相当于只展示自己想要的那部分信息,过滤部分单独显示
    list_filter = ['s_name']
    # 搜索--多了一个搜索框
    search_fields = ['s_name']
    # 分页---分2页,每页2条内容
    list_per_page = 2


# 这里是注册的第一种方式
admin.site.register(Student, StudentAdmin)    

4. Another method of registration - the decorator method @xxx
makes a decorator in the defined class to register

from django.contrib import admin
from stu.models import Student

# Register your models here.
#注册的另一种方法---装饰器方式@xxx
@admin.register(Student)
class StudentAdmin(admin.ModelAdmin):
    # 设置性别为男女显示
    def set_s_gender(self):
        if self.s_gender:
            return '男'
        else:
            return '女'
    # 修改性别字段的描述
    set_s_gender.short_description = '性别'
    # 展示字段列表,只展示id和name字段,同时把StudentAdmin注册进去
    list_display = ['id', 's_name', set_s_gender]
    # 过滤,相当于只展示自己想要的那部分信息,过滤部分单独显示
    list_filter = ['s_name']
    # 搜索--多了一个搜索框
    search_fields = ['s_name']
    # 分页---分2页,每页2条内容
    list_per_page = 2

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325683580&siteId=291194637