Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 338719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:27:20+00:00 2026-05-12T10:27:20+00:00

I have a custom field called HourField, which lets you enter 1:30 to represent

  • 0

I have a custom field called HourField, which lets you enter “1:30” to represent one and a half hours. The clean() method turns it into 1.5 for the FloatField in the model. This all works fine. Now I’m having a problem where when I want to create a formset, that field gets rendered as “1.5” instead of “1:30” which is what I want.

Is there some kind of method similar to clean() but works the other way around?

edit: using another type of storage method is out of the question. I have a few other custom fields I have made which are stored as one type of object, but are entered by the user as a string. I used the example above because it was the most straight forward.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T10:27:20+00:00Added an answer on May 12, 2026 at 10:27 am

    To customize the way a field renders, you need to define a widget as well.

    The following code shows some responsibilities of widgets in django :

    • render the form tag ( superclass TextInput takes care of this )
    • format the value for displaying inside the form field.
      there’s an gotcha here : the value’s class may vary, since you may render cleaned values coming from the field, or uncleaned data coming from a form POST. Hence the test if we have a numeric value in _format_value ; if it’s a string, just leave it as-is.
    • tell if the value has changed, compared to initial data. Very important when you use formsets with default values.

    Please note that the widget’s code is inspired by widgets.TimeInput from the sourcecode of django, which helps being consistent.

    from django import forms
    from django.forms import widgets
    from django.forms.util import ValidationError
    
    class HourWidget(widgets.TextInput):
    
        def _format_value(self, value):
            if isinstance(value, float) or isinstance(value, int):
                import math
    
                hours = math.floor(value)
                minutes = (value - hours) * 60
                value = "%d:%02d" % (hours, minutes)
    
            return value
    
        def render(self, name, value, attrs=None):
            value = self._format_value(value)
            return super(HourWidget, self).render(name, value, attrs)
    
        def _has_changed(self, initial, data):
            return super(HourWidget, self)._has_changed(self._format_value(initial), data)
    
    class HourField(forms.Field):
        widget = HourWidget
    
        def clean(self, value):
            super(HourField, self).clean(value)
    
            import re
            match = re.match("^([0-9]{1,2}):([0-9]{2})$", value)
            if not match:
                raise ValidationError("Please enter a valid hour ( ex: 12:34 )")
    
            groups = match.groups()
            hour = float(groups[0])
            minutes = float(groups[1])
    
            if minutes >= 60:
                raise ValidationError("Invalid value for minutes")
    
            return hour + minutes/60
    

    Please note that 1:20 becomes 1:19, which is due to the loss of precision induced by the use of a float. You might want to change the data type if you don’t want to lose some precision.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom field in WordPress called thumb-url which contains the exact location
I have a custom content type which has a field called location which is
I have a custom field called Current_Address__c which is of datatype textarea. I need
I have a TFS 2010 Work Item Type with a custom field called Requested
In my SharePoint List I have a custom field which is a filtered lookup.
I have a custom list_display field which is responsible for a column of integers
I have a custom list in SharePoint (specifically, MOSS 2007.) One field is a
I have a multiple pages in wordpress all with a single custom field called
I have a phpBB installation. I've added custom profile field called 'insurance number'. Every
Details Ok. So i have a custom field on my posts called Location. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.