Change language

GenericIPAddressField — Django Models

Syntax :

 field_name = models.GenericIPAddressField ( ** options ) 

GenericIPAddressField accepts the following arguments:

 GenericIPAddressField.protocol 

Restricts valid logins to the specified protocol. Valid values ​​are both (default), IPv4 , or IPv6 . Matching is case insensitive.

 GenericIPAddressField.unpack_ipv4 

Unpacks mapped IPv4 addresses as :: ffff: 192.0.2.1 . If this option is enabled, this address will be unpacked to 192.0.2.1 . Disabled by default. Can only be used if protocol is set to both.

Django GenericIPAddressField Model Explanation

GenericIPAddressField illustration using 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.GenericIPAddressField ()

# 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 ge directory eks a new folder named migrations will be created with a file named 0001_initial.py

# Generated by Django 2.2.5 at 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_created = True ,

primary_key = True ,

serialize = False

verbose_name = ’ID’

)),

  ( ’geeks_field’

models.GenericIPAddressField ()),

  ],

  ),

]

Now run,

 Python manage.py migrate 

So the geeks_field GenericIPAddressField is created when you run migrations in the project. This is a field for storing integers.

How to use GenericIPAddressField?

GenericIPAddressField is used to store Pv4 or IPv6 addresses in string format, so basically it’s CharField with IP checking. Let’s try to store the IP address " 0.0.0.0 " in this field.

# model import
# from a geek app

from geeks.models import GeeksModel

 
# instantiate
# IP addresses

d = "0.0.0.0"

 
# instantiate
# GeeksModel

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

geek_object.save ()

Now let’s check this on the admin server. We have created a GeeksModel instance.

Field options

Field parameters — 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 GenericIPAddressField will allow it to store empty values ​​for that table in a relational database. 
Here are the field options and attributes that GenericIPAddressField 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.

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically