[Answered ]-How to write the validator for a model field in Django?

2👍

The validator_factory works because of the closure of the ip_version variable. The inner function returned by factory has access to the ip_version variable. You would use the
ipv4_validator or ipv6_validator function in the validators list for your field.

I would recommend upgrading to Django 1.4 if possible, so that you can use or the GenericIPAddressField. If you need to stick with version 1.3, you could backport the code from Django 1.4. If you stick with your own validators, I wouldn’t worry about duplication too much, just focus on getting two validators that work.

Leave a comment