How to get datetime as string from mysql database in django model query
<p>I am using below query to get fields from mysql database in django 1.9.</p>

<pre><code>event_dict_list = EventsModel.objects.filter(name__icontains = event_name).values('sys_id','name', 'start_date_time', 'end_date_time', 'notes')
</code></pre>

<p>now in the result <code>event_dict_list</code>, <code>start_date_time</code> and <code>end_date_time</code> are appearing in python date time object format as below </p>

<pre><code>'end_date_time': datetime.datetime(2016, 9, 26, 10, 48, 35, tzinfo=&lt;UTC&gt;)
</code></pre>

<p>I want it as a string in <code>YYYY-mm-dd HH:MM:SS</code> format. </p>

<p>One way would be to iterate over the <code>event_dict_list</code> and get date field and then convert it into desired string format. But I wanted to know if there is any way I can specify something in query so that I get the converted date in query output only?</p>

<p>Related question - what is preferred way to store date/datetime in database - as python date time or as string. Way 1 or way 2.<br>
(1) <code>end_date_time = models.DateTimeField(null=False, blank=False)</code><br>
(2) <code>end_date_time = models.CharField(max_length= 128, null=False, blank=False)</code> </p>

<p>You could try using the CAST() command in your query:
<a href="http://stackoverflow.com/questions/2392413/convert-datetime-value-into-string-in-mysql">Convert DateTime Value into String in Mysql</a></p>

<p>The above example kinda shows you how to!</p>

<p>If that doesnt work, the other option is as you stated, iterate over your dates and times and convert as shown here:
<a href="http://stackoverflow.com/questions/10624937/convert-datetime-object-to-a-string-of-date-only-in-python">Convert datetime object to a String of date only in Python</a></p>

python mysql django datetime django-models