This site runs best with JavaScript enabled.

Django MONTH_CHOICES

Django MONTH_CHOICES Django MONTH_CHOICES

Photo by Faisal M on Unsplash

Have you ever wanted to give your model some month choices relating to integers 1-12. I would guess it's pretty common - common enough to be included in django.contrib. Well, it is. Here is a quick tip on how to include it in a model:

from django.db import models
from django.utils.dates import MONTHS


class RevenueGoal(models.Model):
    month = models.PositiveSmallIntegerField(choices=MONTHS.items())
    year = models.PositiveIntegerField()
    goal = models.DecimalField('Revenue Goal', max_digits=8, decimal_places=2)
Share article
Dustin Davis

Dustin Davis is a software engineer, people manager, hacker, and entreprenuer. He loves to develop systems and automation. He lives with his wife and five kids in Utah.