[Django]-How to Model a TimeField in Django?

39👍

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)
👤César

Leave a comment