Package: django-stdfields
Fields I wish were standard in Django. At the moment this is limited to theMinutesField,EnumIntegerFieldandEnumCharField.Install:pip install django-stdfieldsContentsstdfields.forms.MinutesField: use an integer to represent a duration of
minutes and hoursstdfields.fields.EnumIntegerField: makes working withchoicesa bit
easierstdfields.fields.EnumCharField: the same, but forchoiceswith a char
keyMinutesFieldIs an extension of Django’s standarddjango.forms.IntegerField.This field will accept values for a duration in minutes in the formatshh:mmorh.fraction, similar to the way BaseCamp allows you to specify
your time spent on a task as either8:30or8.5. In the latter case only8.25,8.5,8.50and8.75are considered valid inputs.ExampleActions speak louder than words:# models.py
class Task(models.Model):
    time_spent = models.IntegerField()

# forms.py
from stdfields.forms import MinutesField

from models import Task

class TaskForm(forms.ModelForm):
    time_spent = MinutesField(label='How long did it take?')

    class Meta:
        model = TaskYou can use theminutestemplate filter fromstdfieldstagsto render
such a field in the format8:30:{% load stdfieldstags %}
It took me {{ task.time_spent|minutes }} to complete this task.EnumerationI always end up with ugly code when using Django’schoicesargument for
fields. With thestdfields.models.Enumerationclass, I’ve got a handy base
class that allows me to keep things tidy:# models.py
class Color(Enumeration):
    RED = 'R'
    GREEN = 'G'
    BLUE = 'B'

    @classmethod
    def all(cls):
        return [
            (cls.RED, _(u'Red')),
            (cls.GREEN, _(u'Green')),
            (cls.BLUE, _(u'Blue'))
        ]

class Pencil(models.Model):
    color = models.CharField(choices=Color.all(), max_length=Color.max_length())

# views.py
def red_pencils(request):
    pencils = Pencil.objects.filter(color=Color.RED)
    ...
    # Prints 'Showing the Red pencils'
    logging.info('Showing the %s pencils' % (Color.as_display(Color.RED)))That could be shorter. UseEnuminstead:# models.py
class Color(Enum):
    RED = EnumValue('R', 'Red')
    GREEN = EnumValue('G', 'Green')
    BLUE = EnumValue('B', 'Blue')

class Pencil(models.Model):
    color = models.CharField(choices=Color.all(), max_length=Color.max_length())

# views.py
def red_pencils(request):
    pencils = Pencil.objects.filter(color=Color.RED)
    ...
    # Prints 'Showing the Red pencils'
    logging.info('Showing the %s pencils' % (Color.RED_display))EnumCharField and EnumIntegerFieldAnd now we can make working with anEnumeasier with theEnumCharFieldandEnumIntegerFieldmodels fields:# models.py
class Color(Enumeration):
    # same as above

class Pencil(models.Model):
    color = models.EnumCharField(enum=Color, max_length=Color.max_length())This example is basically the same as the above sinceEnumCharFieldis a
subclass of the regular DjangoCharField. By using theenumkeyword
argument of the enum field, the choices will be automatically updated when you
update the enumeration object. And since you’re using the providedmax_lengthmethod ofEnumeration, themax_lengthwill be updated
when needed. Just like in the previous example. The enum fields simply offer
some more clarity when reading the code.EnumIntegerFieldworks exactly the same, but for enumerations with integer
keys. Both fields can be used with South.