TimeField — Django Models

Python Methods and Functions

class TimeField (auto_now = False, auto_now_add = False, ** options) Time, represented by a Python datetime.time object. Accepts the same arguments as DateField. The form uses a TextInput widget. The admin interface also uses a bit of JavaScript.

If you want only time, TimeField is what you need:

class ConversationHistory(models.Model):
    contact_date = models.DateField(_(u"Conversation Date"), blank=True)
    contact_time = models.TimeField(_(u"Conversation Time"), blank=True)

You can take advantage of the auto_now_add option:

class TimeField([auto_now=False, auto_now_add=False, **options])

A time, represented in Python by a datetime.time instance. Accepts the same auto-population options as DateField.

If you use the auto_now_add, it will automatically set the field to now when the object is first created.

class ConversationHistory(models.Model):
    contact_date = models.DateField(_(u"Conversation Date"), auto_now_add=True, blank=True)
    contact_time = models.TimeField(_(u"Conversation Time"), auto_now_add=True, blank=True)
TimeField is a time field that stores time, represented in Python by a datetime.time instance. As the name suggests, this field is used to store a datetime object created in python. The default form widget for this field is a TextInput. The administrator uses two separate TextInput widgets with JavaScript shortcuts.


Syntax

field_name = models.TimeField(auto_now=False, auto_now_add=False, **options)
TimeField has the following extra optional arguments –
TimeField.auto_now
Automatically set the field to now every time the object is saved. Useful for “last-modified” timestamps. Note that the current time is always used; it’s not just a default value that you can override. The field is only automatically updated when calling Model.save(). The field isn’t updated when making updates to other fields in other ways such as QuerySet.update(), though you can specify a custom value for the field in an update like that.

TimeField.auto_now_add

Automatically set the field to now when the object is first created. Useful for creation of timestamps. Note that the current time is always used; it’s not just a default value that you can override. So even if you set a value for this field when creating the object, it will be ignored. If you want to be able to modify this field, set the following instead of auto_now_add=True:
  • For TimeField: default=datetime.time.now – from datetime.now()
  • For TimeField: default=timezone.now – from django.utils.timezone.now()
Note: The options auto_now_add, auto_now, and default are mutually exclusive. Any combination of these options will result in an error.


Field options

Field parameters are arguments given to each field to apply some kind of constraint or pass a specific characteristic to a specific field. For example, adding the null = True argument to the TimeField will allow it to store empty values ​​for that table in a relational database.
Here are the field parameters and attributes that TimeField can use.

Field Options Description
Null If True , Django will store empty values ​​as NULL in the database. Default is False.
Blank If True , the field is allowed to be blank ... Default is False.
Choices An iterable (eg, a list or tuple) of 2-tuples to use as choices for this field.
Default The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created.
help_text Extra “help” text to be displayed with the form widget. It's useful for documentation even if your field isn't used on a form.
primary_key If True, this field is the primary key for the model .
Unique If True, this field must be unique throughout the table.








Tutorials