Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django DateField without year

Is it possible to have a DateField without the year? Or will I have to use a CharField? I am using the admin to edit the models.

like image 388
Ali Avatar asked Jun 18 '10 05:06

Ali


3 Answers

This works for me:

data_sorted = sorted(My_Model.objects.all(), key=lambda e: e.myDataField.strftime("%m-%d"))
like image 133
Daniel Gomez Rico Avatar answered Oct 17 '22 23:10

Daniel Gomez Rico


You need to decide if you are ok with a regular date object on the model (and the DB) and will just customize the form widgets or if you want the data store to have the specific data.

If you only want to have forms display day and month, you need to write a custom widget and hook it up to the form in the ModelAdmin class. Django's documentation has a pretty good introduction to writing custom fields and custom widgets

In both cases it's much easier to subclass an existing Model / widget and change the behavior you want.

like image 33
Arthur Debert Avatar answered Oct 17 '22 23:10

Arthur Debert


A DateField is a whole date. If you only care about the month and day then use __month and __day when querying.

like image 1
Ignacio Vazquez-Abrams Avatar answered Oct 17 '22 23:10

Ignacio Vazquez-Abrams