This site runs best with JavaScript enabled.


Photo by Faisal M on Unsplash

Short tip on getting month choices from django.contrib

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:

1from django.db import models
2from django.utils.dates import MONTHS
5class RevenueGoal(models.Model):
6 month = models.PositiveSmallIntegerField(choices=MONTHS.items())
7 year = models.PositiveIntegerField()
8 goal = models.DecimalField('Revenue Goal', max_digits=8, decimal_places=2)

Discuss on TwitterEdit post on GitHub

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.

Join the Newsletter

Dustin Davis