11

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.

Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
Ali
  • 4,311
  • 11
  • 44
  • 49

3 Answers3

2

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.

Arthur Debert
  • 10,237
  • 5
  • 26
  • 21
1

This works for me:

data_sorted = sorted(My_Model.objects.all(), key=lambda e: e.myDataField.strftime("%m-%d"))
Daniel Gomez Rico
  • 15,026
  • 20
  • 92
  • 162
1

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

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • so if I wanted to retrieve a queryset of all the dates after today it would be something like: today = datetime.date.today() Object.object.filter(date__day=today).exclude() ? Thanks – Ali Jun 18 '10 at 17:32