DurationField — Django Models

Python Methods and Functions

Syntax

 field_name = models.DurationField ( ** options ) 

Django DurationField Model Explanation

Illustration of a DurationField using an example. Consider a project named pythonengineering that has an application named geeks .

Refer to the following articles to check how to create a project and an app in Django.

Enter the following code into the models.py file of the geeks application.

Add the geek app to INSTALLED_APPS

from django.db import models

from django.db.models import Model

# Create your models here.

  

class GeeksModel (Model):

geeks_field = models.DurationField ()

# Application definition

 

INSTALLED_APPS = [

  ' django .contrib.admin' ,

'django.contrib.auth' ,

  ' django.contrib.contenttypes' ,

'django.contrib.sessions' ,

'django.contrib.messages' ,

'django.contrib.staticfiles' ,

'geeks' ,

]

Now when we run the makemigrations command from the terminal,

 Python manage.py makemigrations 

In the geeks directory a new folder named migrations will be created with a file named 0001_initial.py

# Generated by Django 2.2.5 2019- 09-25 06:00

 

from django.db import migrations, models

 

class Migration (migrations.Migration):

 

initial = True

  

dependencies = [

]

 

operations = [

migrations.CreateModel (

name = 'GeeksModel' ,

  fields = [

( 'id'

models.AutoField (

auto_cr eated = True ,

primary_key = True ,

serialize = False

  verbose_name = ' ID'

)),

( 'geeks_field' , models.DurationField ()),

],

),

]

Now run

 Python manage.py migrate 

So geeks_field DurationField is generated when migrations are run in the project. This field is used to store the python datetime.timedelta object.




How to use DurationField?

DurationField is used to store python instance datetime.timedelta in the database. You can store any type of time or date based duration in the database. To learn more about datetime.timedlta check

# model import
# from geek apps

from geeks.models import GeeksModel

 
# import date and time module

import datetime

 
# create an instance
# datetime.timedelta

d = datetime.timedelta (days = - 1 , seconds = 68400 )

 
# instantiate
# GeeksModel

geek_object = GeeksModel.objects.create (geeks_field = d)

geek_object.save ()

Now let's let's check it on the administrative server. We have created a GeeksModel instance. 

Field options

Field options — they are arguments given to each field to apply some constraint or to convey a particular characteristic to a particular field. For example, adding the null = True argument to the DurationField will allow it to store empty values ​​for that table in a relational database. 
Here are the field options and attributes that the DurationField can use.

Field Options Description
Null If True , Django will store empty values ​​as NULL in the database. Default is False.
Blank If True , the field is allowed to be blank ... Default is False.
Choices An iterable (eg, a list or tuple) of 2-tuples to use as choices for this field.
Default The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created.
help_text Extra “help” text to be displayed with the form widget. It's useful for documentation even if your field isn't used on a form.
primary_key If True, this field is the primary key for the model .
Unique If True, this field must be unique throughout the table.





Get Solution for free from DataCamp guru