I have modeled a class called ConversationHistory. Whenever an instance is created I wish to set the current date and current time.
class ConversationHistory(models.Model):
contact_date = models.DateField(_(u"Conversation Date"), blank=True)
contact_time = models.DateTimeField(_(u"Conversation Time"), blank=True)
def __init__(self, *args, **kwargs):
super(ConversationHistory, self).__init__(*args, **kwargs)
self.contact_date = datetime.datetime.now()
self.contact_time = datetime.datetime.now()
The idea is that the user can later still adjust the date and time as two different fields.
I am now a bit lost how to make the time field only to show and accept time, rather than date and time. I recon it is not possible to have a time field without datetime, but then how would I show only the time in the form?
If you want only time, TimeField is what you need:
You can take advantage of the
auto_now_addoption:If you use the
auto_now_add, it will automatically set the field to now when the object is first created.