1

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')
Mathew Berg
  • 28,625
  • 11
  • 69
  • 90
Patrick
  • 21
  • 5
  • posible duplicate of http://stackoverflow.com/questions/4876370/django-date-format-dd-mm-yyyy – Pavan Gupta Jun 19 '14 at 12:48
  • Django Model supports strftime so you could check [here](https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior) for your disired format – Pavan Gupta Jun 19 '14 at 12:53
  • But I still dont know how to change the format of my DateField. – Patrick Jun 19 '14 at 13:08
  • If I can understand you question correctly you want to store total no of weeks and corresponding year not the current week no. right ? – Pavan Gupta Jun 19 '14 at 13:30

1 Answers1

0

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.

Related questions: here and here

Community
  • 1
  • 1
ruddra
  • 50,746
  • 7
  • 78
  • 101
  • But I dont want to insert date in DD/MM/YYYY but in format like WW/YYYY the WW characters represent week of the year. – Patrick Jun 19 '14 at 15:32
  • can you give an example?? – ruddra Jun 19 '14 at 16:30
  • I would like to use only weeks and year in my project which will be defined in database through DateField. Like when I register an user and I only define there the week of the year of his registration and not the full date. – Patrick Jun 19 '14 at 20:14
  • I'll give you my feedback after few tests. – Patrick Jun 21 '14 at 11:10