V Manikandan
V Manikandan

Reputation: 370

django postgres datetime field to python datetime

I have the following model class

class Transaction(models.Model):
    quantity = models.IntegerField(default=0)
    sell_time = models.DateTimeField()

When I fetch "sell_time" from the model, I am getting datetime in the following format

2014-10-01 08:09:46.251563+00:00

my question is, if it is not in the format like

year-month-day hour:minutes:seconds

how can I convert to python datetime object like

datetime.datetime(2014, 10, 1, 08, 09, 46, 540535)

many thanks

Upvotes: 0

Views: 1037

Answers (2)

e-nouri
e-nouri

Reputation: 2626

You are seeing that because you did not set the correct time zone. Use the UTC time and then you can format .strftime("format") the time accordingly to each locale, you can install pytz and enable it in the settings to handle all the hustle. Always use UTC datetime object because then you can get what ever time you want knowing the zone your user is in. Documentation from Django

Upvotes: 0

Klaus D.
Klaus D.

Reputation: 14369

Did you print it? If you print a datetime object, it is serialized to a string but it is a datetime. You can use it as any other datetime.

Upvotes: 1

Related Questions