Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit fields to a specific family (IPv6, IPv4, or both) #88

Closed
gmazoyer opened this issue Apr 18, 2019 · 1 comment
Closed

Limit fields to a specific family (IPv6, IPv4, or both) #88

gmazoyer opened this issue Apr 18, 2019 · 1 comment

Comments

@gmazoyer
Copy link

It could be a good idea to add a named parameter to fields in order to restrict them to a specific address family.

Of course, I'm not talking about an address family restriction on a database level but on a model level. In this way a model could include only IPv4 (or IPv6) address fields.

By default, a field could accept both IPv6 and IPv4 addresses (or networks) or if given a named parameter (family for instance), it will validate the value passed to it, checking it it from the right address family.

I can work on a PR if this issue is proposal is legit.

@jimfunk
Copy link
Owner

jimfunk commented Apr 19, 2019

That's not actually a function of the database field. This is what validators are for. It's really easy to do, too:

@deconstructible
class AddressVersionValidator(BaseValidator):
    compare = lambda self, value, version: value.version != version
    message = 'Must be an IPv%(limit_value)s address.'
    code = 'address_version'

Then you can add the validator to the field definition:

myaddress = InetAddressField(validators=[AddressVersionValidator(4)])

@jimfunk jimfunk closed this as completed Apr 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants