Django 13-Manually set the primary key of the data table through the pk value

I. Introduction

When adding a new database table through the Model layer of the django framework, if no primary key is set for any table field in the table field to be added, the framework will add a table field id by default and set the table field id as the primary key.

Then, if we want to set the primary key of one of the newly added table field A by ourselves, we need to add a parameter primary_key and the parameter primary_key value must be equal to True in the method called corresponding to this table field A.

detail:

①. The parameter primary_key, generally referred to as pk, commonly known as the primary key.

Second, the implementation process of the default id primary key

1. First, add a class [User] in the absolute path of the project [helloworld/hello/models.py], and add related class attributes.

# Add a new user table, the table name is user, there are three table fields: user_name, user_psw, user_email three table field data types are string types

class User(models.Model):

    user_name = models.CharField(max_length=30)

    user_psw = models.CharField(max_length=30)

    user_email = models.CharField(max_length=30)

2. Next, execute the command line [python manage.py makemigrations] under the absolute path of the project [helloworld/].

detail:

①. Under the root directory [helloworld/] of the project [helloworld], the command line [python manage.py makemigrations] can be executed to take effect, because manage.py only exists in the root directory;

②. [makemigrations] in the command line [python manage.py makemigrations] is actually passed to manage.py as the input parameter value of one of the input parameters, this is a special way of writing the command line; the relevant knowledge points are shown in the screenshot below :

3. Next, execute the command line [python manage.py migrate] under the absolute path of the project [helloworld/].

Three, artificially set the primary key of the data table through the pk value

1. First, add a class [Role] in the absolute path of the project [helloworld/hello/models.py], and add related class attributes.

# Add a new role table. The table name is hello_role. It has three table fields: role_name, role_level, and role_intro. The data types of the three table fields are all string types.

class Role(models.Model):

    role_name = models.CharField(max_length=30, primary_key=True)  # 角色名;这个表字段role_name设置为主键,让角色名是不重复的;

    role_level = models.CharField(max_length=30)  # 角色等级

    role_intro = models.CharField(max_length=30)  # 角色简介

detail:

①. The role of the primary key: to ensure that the value of each primary key in each piece of data in the data table is unique;

2. Next, execute the command line [python manage.py makemigrations] under the absolute path of the project [helloworld/]

3. Next, execute the command line [python manage.py migrate] under the absolute path of the project [helloworld/].

Guess you like

Origin blog.csdn.net/LYX_WIN/article/details/114374305