Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: IPv4 only for GenericIPAddressField

Tags:

python

django

Using Django's GenericIPAddressField - https://docs.djangoproject.com/en/1.10/ref/models/fields/#genericipaddressfield for my model like this:

group_address = models.GenericIPAddressField()

If invalid value is inputted, this returns a message:

Enter a valid IPv4 or IPv6 address.

Now in my case the field only accepts IPv4 addresses. I would like to remove IPv6 from the message.

Is it possible to tune GenericIPAddressField to only handle IPv4 or overwrite the error message?

like image 523
0leg Avatar asked Dec 13 '16 14:12

0leg


People also ask

How do you store IP address in Django?

GenericIPAddressField is a field which stores an IPv4 or IPv6 address, in string format (e.g. 192.0. 2.30 or 2a02:42fe::4). The default form widget for this field is a TextInput.

What is difference between null and blank in Django?

null is purely database-related, whereas blank is validation-related(required in form). If null=True , Django will store empty values as NULL in the database . If a field has blank=True , form validation will allow entry of an empty value . If a field has blank=False, the field will be required.

What is Max_length in Django?

The maximum length (in characters) of the field. The max_length is enforced at the database level and in Django's validation using MaxLengthValidator . If you are writing an application that must be portable to multiple database backends, you should be aware that there are restrictions on max_length for some backends.

What is Related_name in Django?

The related_name attribute specifies the name of the reverse relation from the User model back to your model. If you don't specify a related_name, Django automatically creates one using the name of your model with the suffix _set. Syntax: field_name = models.Field(related_name="name")


1 Answers

I have found out that I have to set protocol attribute on the field:

group_address = models.GenericIPAddressField(protocol='IPv4')

Now the message looks like:

Enter a valid IPv4 address.
like image 145
0leg Avatar answered Oct 07 '22 07:10

0leg