0

If I use something like

   year_published = models.DateField()

then I have to enter in a day and month, in addition to a year. But in this field I only want to enter a year. Likewise, I only want to enter a month in

   month_published = models.DateField()

Any idea how to do this?

henrich
  • 597
  • 1
  • 8
  • 22
  • well you cannot do that with DateField you need CharField to do what you are trying to do – Exprator Jul 09 '17 at 16:41
  • Thanks, but I want to be able to sort the publications by month and year and I can't do that if the data is in a CharField. I could use integer fields, but I was wondering if there was a more straightforward or elegant solution. The problem is that articles (which is what I'm putting in the database) tend to only have publication dates in months and years, but not days. – henrich Jul 09 '17 at 16:46

1 Answers1

0

If you only store the year, how will you sort on Month and Date? Instead, store the full datetime object or split out the full date into three fields. As already mentioned, these would normally be Char fields, but can be Int and can be converted back to date to sort and compare/diff. See:

How to convert integer into date object python?

Or simply store the full date natively and then get the chunks you need from it as you need it. You might check out the Pendulum Package which opens up a lot more options on date operations.

wedward
  • 363
  • 2
  • 11