简体   繁体   中英

Django field DateField with year only or with year and month etc. with filtering

I have a question regarding the DateField. Is there any possibility for the field to accept only a year or a year and a month? I want to add an incomplete date to the database and filter by this field. I download books from API and the date is not always full there. I want to download the results, save them to the database and display a list of these objects on the page with an incomplete date or a complete date.

I think it's better to complete the date received from API and you will have more control over that. You can filter by year and month .

You can have a function to check and complete the date received from API and store it.

Books.objects.filter(date__year=today.year, date__month=today.month).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM