Django custom widget to split an IntegerField into hours and seconds fields in a Model Form

旧巷老猫 提交于 2020-01-12 10:30:28

问题


I am storing time duration as minutes in an IntegerField. In my front end form (Model Form), I want to split the input into one field for minutes and one field for hours. I also want to be able to use the InLineFormset. Can this be done in a nice way? I could use javascript, but that does not seeml like a good solution to me, or is it?


回答1:


You want to use a MultiWidget. I'd suggest that you override the field itself. Have a look at the docs for creating custom fields.

If you can, it might be worth storing your data as a floating point representing a python timedelta. You may want to add a few custom methods to your field. Using timedelta will be useful if you want to do any calculations with your field.

You'll probably want to do a bit of reading on form validation too.

I've tested the following hours, mins, sec multiwidget with float based custom field on Django 1.4 -

from datetime import timedelta
from django import forms
from django.db import models
from django.core import exceptions, validators


def validate_timedelta(value):
    if not isinstance(value, timedelta):
        raise exceptions.ValidationError('A valid time period is required')


class SplitHoursMinsWidget(forms.widgets.MultiWidget):

    def __init__(self, *args, **kwargs):
        widgets = (
            forms.TextInput(),
            forms.TextInput(),
            forms.TextInput(),
        )
        super(SplitHoursMinsWidget, self).__init__(widgets, *args, **kwargs)

    def decompress(self, value):
        if value:
            return [value.seconds // 3600, (value.seconds // 60) % 60,
                    value.seconds % 60]
        return [None, None, None]

    def format_output(self, rendered_widgets):
        return ("HH:MM:SS " + rendered_widgets[0] + ":" + rendered_widgets[1] +
                 ":" + rendered_widgets[2])

    def value_from_datadict(self, data, files, name):
        hours_mins_secs = [
            widget.value_from_datadict(data, files, name + '_%s' % i)
            for i, widget in enumerate(self.widgets)]
        try:
            time_delta = (timedelta(hours=float(hours_mins_secs[0])) +
                          timedelta(minutes=float(hours_mins_secs[1])) +
                          timedelta(seconds=float(hours_mins_secs[2])))
        except ValueError:
            return None
        else:
            return time_delta


class TimeDeltaFormField(forms.Field):

    widget = SplitHoursMinsWidget


class TimeDeltaField(models.Field):

    __metaclass__ = models.SubfieldBase

    description = "Field to hold a python timedelta object"

    default_validators = [validate_timedelta, ]

    def to_python(self, value):
        if value in validators.EMPTY_VALUES or isinstance(value, timedelta):
            return value
        try:
            return timedelta(seconds=float(value))
        except:
            raise exceptions.ValidationError('A valid time period is required')

    def get_prep_value(self, value):
        return float(value.days * 86400 + value.seconds)

    def get_internal_type(self):
        return 'FloatField'

    def formfield(self, **kwargs):
        defaults = {'form_class': TimeDeltaFormField}
        defaults.update(kwargs)
        return super(TimeDeltaField, self).formfield(**defaults)

It's worth noting that this doesn't provide any useful input level validation (it's possible to enter over 60 in both the minutes and seconds input elements). Perhaps that could be best resolved with javascript.



来源:https://stackoverflow.com/questions/13824266/django-custom-widget-to-split-an-integerfield-into-hours-and-seconds-fields-in-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!