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?
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.
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.
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.
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")
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With