field_name = models.GenericIPAddressField ( ** options )
GenericIPAddressField accepts the following arguments:
Restricts valid logins to the specified protocol. Valid values are both (default), IPv4 , or IPv6 . Matching is case insensitive.
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
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.
Now when we run the
makemigrations command from the terminal,
Python manage.py makemigrations
ge directory eks a new folder named migrations will be created with a file named
Python manage.py migrate
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.
Now let’s check this on the admin server. We have created a GeeksModel instance.
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.
|If True , Django will store empty values as NULL in the database. Default is False.
|If True , the field is allowed to be blank ... Default is False.
|An iterable (eg, a list or tuple) of 2-tuples to use as choices for this field.
|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.
|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.
|If True, this field is the primary key for the model .
|If True, this field must be unique throughout the table.