Date and time format DateTimeField in Django

  • When creating a django model, there are three types of DateTimeField, DateField and TimeField that can be used to create date fields, and their values ​​correspond to the three objects of datetime(), date(), and time() respectively. These three fields have the same parameters auto_now and auto_now_add. It looks easy on the surface, but it is easy to make mistakes in actual use. The following are some points to pay attention to. The default value of DateTimeField.auto_now is false. When set to true, it can set its value to the current time when saving the field, and
  • When creating a django model, there are three types of DateTimeField, DateField and TimeField that can be used to create date fields, and their values ​​correspond to the three objects of datetime(), date(), and time() respectively. These three fields have the same parameters auto_now and auto_now_add. It looks easy on the surface, but it is easy to make mistakes in actual use. The following are some points to pay attention to.

    DateTimeField.auto_now

    The default value of this parameter is false. When it is set to true, the value of the field can be set to the current time when the field is saved, and it will be automatically updated every time the model is modified. Therefore, this parameter is very convenient in scenarios where the "last modification time" needs to be stored. It should be noted that when this parameter is set to true, it does not simply mean that the default value of the field is the current time, but means that the field will be "forced" to update to the current time, and you cannot manually assign a value to the field in the program; if Using the admin manager brought by django, this field is read-only in the admin.

    DateTimeField.auto_now_add

    The default value of this parameter is also False. When it is set to True, when the model object is created for the first time, the value of the field will be set to the time of creation. When the object is modified later, the value of the field will not be updated. This property is usually used in scenarios where the "creation time" is stored. Similar to auto_now, auto_now_add is also mandatory. Once set to True, the field cannot be manually assigned in the program, and the field will become read-only in admin.

     

    datetime field in admin

    When auto_now and auto_now_add are set to True, doing so will cause the field to become editable=False and blank=True. editable=False will cause the field not to be rendered in the admin, blank=Ture will allow no value to be entered in the form. At this point, if the date and time field is forcibly added to the fields or fieldset of the admin, the program will report an error and the admin cannot open it; if you want to see the date and time when modifying the object in the admin, you can add the date and time field to In readonly_fields of the admin class:

    class YourAdmin(admin.ModelAdmin): readonly_fields = ('save_date', 'mod_date',)admin.site.register(Tag, YourAdmin)  How to set creation time as "default current" and modifiable

    So here comes the problem. In practical scenarios, it is often desirable to set the current value by default at the creation time of the object, and to modify it in the future. How to achieve this requirement?

    All model fields in django have a default parameter, which is used to set a default value for the field. You can replace auto_now=True or auto_now_add=True with default=timezone.now. timezone.now corresponds to django.utils.timezone.now(), so it needs to be written as follows:

    from django.db import models import django.utils.timezone as timezoneclass Doc(models.Model): add_date = models.DateTimeField('save date', default = timezone.now) mod_date = models.DateTimeField('last modified date', auto_now = True

     

    When the html page reads the DateTimeField field from the database, the displayed time format is inconsistent with the format stored in the database. For example, the database field content is 2016-06-03 13:00:00, but the page displays Apr. 03, 2016 , 1 pm

    In order to display the same page and database, you need to format the time on the page, you need to add  a filter similar to <td>{{ infor.updatetime|date:"Ymd H:i:s"  }}</td>. Refresh the page to display normally.

  • The above is the content of  DateTimeField in date and time format in Django. For more content of DateTimeField  date  format  and time  in Django  , please use the search function at the top right to obtain relevant information.

Guess you like

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