How can I insert only the number of weeks and year to the date in the Django model? Because now, can I only add the normal date in format DD/MM/YYYY.
class Status(models.Model):
date = models.DateField('Date')
How can I insert only the number of weeks and year to the date in the Django model? Because now, can I only add the normal date in format DD/MM/YYYY.
class Status(models.Model):
date = models.DateField('Date')
You have to understand that datefield stores date, which is a object. So, in whichever format you want to input, you have to convert it to the date object. For example: If you insert a date in ww/yyyy:
forms.py
date_value= form.CharField() #When you render this form in template, use JQuery Mask to keep format.
#lets say it will get input like WW/YYYY
views.py
date_value = datetime.strptime(str(request.POST['date_value'])+'/1', '%W/%Y/%w') #here week count starts from Monday
new_status= Status()
new_status.date= date_value
new_status.save()
More details about .strptime here.