This site runs best with JavaScript enabled.

Django MONTH_CHOICES

Short tip on getting month choices from django.contrib

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)

Discuss on 𝕏 Edit post on GitHub