qg_java_17137
qg_java_17137

Reputation: 3569

How to pass the time to the DateTimeField?

In my models.py I have a GTTime model:

class GTTime(models.Model):
    gt_time = models.DateTimeField(null=True)
    ctime = models.DateTimeField(auto_now_add=True)
    uptime = models.DateTimeField(auto_now=True)

And in my views.py, how to add the time to in the gt_time?

I tried use the time.time() to pass the timestamp, but there will report error:

gt = models.GTTime(gt_time=time.time())
gt.save()

Error: TypeError: expected string or buffer

Upvotes: 0

Views: 388

Answers (1)

aircraft
aircraft

Reputation: 26886

From the django datetime doc

A date and time, represented in Python by a datetime.datetime instance. Takes the same extra arguments as DateField.

but you you gives a time instance, take the bellow code:

gt = models.GTTime(testTime=datetime.now())

gt.save()

Upvotes: 3

Related Questions