I would like to repopulate a DateField in my database ..for example for this "EXPIRYDATE": "2016-Sep-02",
json value I get Feb. 2, 2016
in my database
expirydate = models.DateField(auto_now=True, blank=True, null=True)
This will try to loop through two objects in a list one in French and one in English so I'll try to update the expirydate
field but it's not the proper value.
for lang in [0,1]:
jobs_lang = data[lang]['jobs']
for job in jobs_lang:
emploi= Job.objects.filter(jobref=job['JOBREF'])
if (emploi[0].expirydate != job['EXPIRYDATE']):
emploi[0].expirydate = job['EXPIRYDATE']
emploi[0].save()
答案 0 :(得分:1)
If you know that the date format is going to be consistent, you can use the datetime
module's strptime
function to parse the date string:
import datetime
# the date_format should match what's provided in the JSON
date_format = '%Y-%b-%d'
emploi[0].expirydate = datetime.datetime.strptime(job['EXPIRYDATE'], date_format)
Here, date_obj
will be an object you save via the Django ORM into the database.
Also see https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior for alternate format strings.