开发者

Django DateField without year

开发者 https://www.devze.com 2023-01-04 02:21 出处:网络
Is it possible to have a DateField without the year? Or will I have to use a CharField? I am using开发者_StackOverflow中文版 the admin to edit the models.You need to decide if you are ok with a regula

Is it possible to have a DateField without the year? Or will I have to use a CharField? I am using开发者_StackOverflow中文版 the admin to edit the models.


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.


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


This works for me:

data_sorted = sorted(My_Model.objects.all(), key=lambda e: e.myDataField.strftime("%m-%d"))
0

精彩评论

暂无评论...
验证码 换一张
取 消