Twenty-four of django--addition of creation time and update time

I. Introduction

When we add an article through the admin management backend, there are generally two fields [create time] and [last update time], and we hope that the assignment rules for these two fields are as follows:

  • After the first new article content is successfully added: the values ​​of [Creation Time] and [Last Update Time] will be automatically assigned to the system time when the new article is successfully added, at this time the values ​​of [Create Time] and [Last Update Time] Must be equal
  • Every time the content of the article is successfully modified: the value of [Creation Time] remains the same as the system time when the new article is successfully added, and the value of [Last Update Time] will be automatically updated and assigned to the system time when the content of the article is successfully modified. , At this time, the values ​​of [Create Time] and [Last Update Time] must not be equal;

detail:

①. In the design stage of each data table, in fact, there should be two fields of [creation time] and [last update time], because these two fields are often used as conditions for data sorting and display.

 

Two, enter the parameter auto_now and enter the parameter auto_now_add

In each model class, there are three types of DateTimeField and DateField and TimeField. These three types can be used to create data table fields of three different date types respectively;

These three types have the same parameters: auto_now and auto_now_add;

detail:

①. When the value of the input parameter auto_now or auto_now_add in any of the three categories is set to True, it will cause: the editable value of the input parameter in the class becomes False, and the value of the input parameter blank in the class changes Is True.

②. The editable value of the input parameter is False, which means: the corresponding table field will not be displayed on any page in the admin management background, for example, it will not be displayed on the newly added page or on the modified page;

③. The value of the input parameter blank is True, which means: Allow users not to enter the value of the corresponding table field in the add/edit page;

④. The values ​​of input parameter auto_now and input parameter auto_now_add cannot be set to True at the same time;

1. Enter the relevant knowledge points of auto_now

The default value of the input parameter auto_now is False;

When the value of the input parameter auto_now is set to True, it means: when a new piece of data is successfully modified, the value of the table field corresponding to the input parameter auto_now is set to the system time when the modification is successful;

2. Enter the relevant knowledge points of auto_now_add

The default value of the input parameter auto_now_add is False;

When the value of the input parameter auto_now_add is set to True, it means that when a new piece of data is created successfully, the value of the table field corresponding to the input parameter auto_now_add is set to the system time when the creation is successful, and when the new data is modified later, The value of the table field will not be updated;

 

Three, the complete operation process

3.1. The first step: create a model class

# 新增一个Article类

class Article(models.Model):

    '''文章'''

    title = models.CharField(max_length=30)  # 标题

    body  = models.TextField() # 正文(因为文章的正文字数会很多,所以需要使用该类TextField)

    author = models.CharField(max_length=10) # 作者

    create_time = models.DateTimeField(auto_now_add=True)  # 创建时间

    update_time = models.DateTimeField(auto_now=True)      # 更新时间

 

3.2. Step 2: Create a data table

Create it through migration-related command lines, such as [python manage.py makemigrations] and [python manage.py migrate]. The related operations are mentioned in the previous blog, and I will not describe them in detail here;

 

 

3.3. Step 3: Configure in [admin.py]

class ControllerArticle(admin.ModelAdmin):

    list_display = ("title","author","body","create_time","update_time")

    search_fields = ("title",)





admin.site.register(models.Article,ControllerArticle)

3.4. Step 4: Successfully log in to the admin management background

3.5. Step 5: Successfully add an article

 

 

3.6. Step 6: Successfully update an article

 

 

 

 

Guess you like

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