# Django DurationField for only hours and minutes?

**URL:** <https://forum.djangoproject.com/t/django-durationfield-for-only-hours-and-minutes/10575>\
**Category:** Using Django\
**Created:** [November 19, 2021, 12:20am UTC](https://forum.djangoproject.com/t/django-durationfield-for-only-hours-and-minutes/10575 "2021-11-19T00:20:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![masterj\_02](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@masterj\_02](https://forum.djangoproject.com/u/masterj_02)\
**Post date:** [November 19, 2021, 12:20am UTC](https://forum.djangoproject.com/t/django-durationfield-for-only-hours-and-minutes/10575/1 "2021-11-19T00:20:03Z")

</div>

Hi everyone! I am working with a `ModelForm` where I have a field that is stored in the database as a `DurationField`

Now on user input, when a user enters `1:30` I would like this to register as 1 hour and 30 minutes, but the `DurationField` stores this in the database as `00:01:30` which is 1 minute and 30 seconds.

When I go into my django shell, I see the output of the field as `datetime.timedelta(seconds=90) ` I am trying to figure out a way to convert this to either `datetime.timedelta(minutes=90)` or even parse `00` on the end of input just to store this correctly in the DB

I’m not sure why the timedelta defaults to `seconds` ?

I’ve never worked with a `DurationField` field before, and appreciate any guidance

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 19, 2021, 3:35am UTC](https://forum.djangoproject.com/t/django-durationfield-for-only-hours-and-minutes/10575/2 "2021-11-19T03:35:06Z")

</div>

It might be helpful if you posted the relevent code / templates / settings / whatever that is handling this field.

---

<div class="post-metadata">

**Author:** ![masterj\_02](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@masterj\_02](https://forum.djangoproject.com/u/masterj_02)\
**Post date:** [November 19, 2021, 5:46am UTC](https://forum.djangoproject.com/t/django-durationfield-for-only-hours-and-minutes/10575/3 "2021-11-19T05:46:46Z")

</div>

I’m Just using a simple crispy form using a `DurationField` which just renders in the form as a text field.

```auto

class TimeForm(ModelForm):
    setup_duration = forms.DurationField(required=False)

```

I attempted to build a custom parser such as…

```auto
def duration_string(duration):

    days = duration.days
    seconds = duration.seconds

    minutes = seconds // 60

    hours = minutes // 60
    minutes = minutes % 60

    dur_string = f'{hours}:{minutes}:00'
    if days:
        dur_string = 'f{days} + dur_string'
    return dur_string

class CustomDurationField(forms.DurationField):
    def prepare_value(self, value):
        if isinstance(value, datetime.timedelta):
            return duration_string(value)
        return value

class TimeForm(ModelForm):
    setup_duration = CustomDurationField()

```

But when a user inputs `1:30` into the form field it still saves in the DB `DurationField` as minutes:seconds, which I am looking for hours:minutes.

The above user input example of `1:30` would store in my DB as `datetime.timedelta(seconds=90)`, which I need it stored as `datetime.timedelta(minutes=90)`

---

<div class="post-metadata">

**Author:** ![nemecek\_f](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nemecek_f/32/668_2.png) [@nemecek\_f](https://forum.djangoproject.com/u/nemecek_f)\
**Post date:** [November 19, 2021, 9:36am UTC](https://forum.djangoproject.com/t/django-durationfield-for-only-hours-and-minutes/10575/4 "2021-11-19T09:36:51Z")

</div>

Perhaps it would make more sense to store the number of minutes in the DB? But you would need extra logic to handle data coming from the form.

Or maybe something like custom `clean_duration` method that would convert the entered time into hours and minutes?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 19, 2021, 1:22pm UTC](https://forum.djangoproject.com/t/django-durationfield-for-only-hours-and-minutes/10575/5 "2021-11-19T13:22:05Z")

</div>

A f[orms.DurationField](https://docs.djangoproject.com/en/3.2/ref/forms/fields/#durationfield) uses the [`parse_duration` method](https://docs.djangoproject.com/en/3.2/ref/utils/#django.utils.dateparse.parse_duration) to convert the entered data into a Duration.

This gives you a couple options:

- As mentioned above, you could do a clean\_setup\_duration method to re-parse the entered value.
- You could create a custom `DurationFieldish` class that implements a custom `to_python` method.
- You could change the form field type to a CharField, and perform the parsing yourself based on your requirements.
