Home > Back-end >  Django model datefield with YYYY-MM instead YYYY-MM-DD
Django model datefield with YYYY-MM instead YYYY-MM-DD

Time:02-23

I would like to have a model YYYY_MM which stores

  • 2022-01
  • 2022-02
  • etc

I thought the best method would be to use the DateField but it looks like that YYYY-MM-DD is enforced?

CodePudding user response:

Could you just store YYYY-MM-DD and display YYYY_MM to the front-end?

CodePudding user response:

DateField stores a python date. But 2022-01 is not a date it is a month.

To solve a similar problem before, I have used the convention of storing the first day of a certain time period. In my case this was first day of week to store weeks, but it should be the same thing.

Check out this answer for an idea of how to do that https://stackoverflow.com/a/45266909/15038439

  • Related