Django GenericIPAddress字段未验证输入 [英] Django GenericIPAddress field is not validating input

查看:904
本文介绍了Django GenericIPAddress字段未验证输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的Django模型

  class AccessPointIPAddress(models.Model):
'''模型用于存储AccessPoint IP地址。'''
ap = models.ForeignKey(AccessPoint,related_name ='ip_addresses')
ip_address = models.GenericIPAddressField(protocol ='IPv4')
datetime = models .DateTimeField(auto_now_add = True)

class Meta:
ordering = ['datetime']
get_latest_by ='datetime'
/ pre>

我假设django的 GenericIPAddressField 做了一些字符串验证,一个字符串确实是一个有效的IP地址。我也读了django的来源,它确实有一些验证功能绑定到 GenericIPAddressField



但是当我尝试运行在django的shell上:

 #假设* ap *是有效的AccessPoint实例
#注意ip_address不是有效的IP ADDRESS
>> AccessPointIPAddress.objects.create(ap = ap,ip_address ='xxxxxx123123 ----')
< AccessPointIPAddress:ap xxxxxx123123 ---- 2015-05-18 12:39:25.491811>

我希望它会引起某种ValueError或验证错误,因为给定的ip-address xxxxxx123123 ---- 不是有效的IP地址。



我在这里缺少什么?还是django的这部分破碎?目前使用Django 1.6.5

解决方案

https://docs.djangoproject.com/en/1.8/ref/validators/#how-validators-are-run


有关验证器如何在表单中运行的更多信息,以及如何在模型中运行对象,请参阅表单验证。请注意,保存模型时,验证器不会自动运行,但是如果您使用的是ModelForm,那么它将在表单中包含的任何字段上运行验证器。有关模型验证与表单交互的信息,请参阅ModelForm文档。


可以覆盖save()方法并执行一个full_clean()在模型实例上,如文档中所述:
https://docs.djangoproject.com/en/1.8/ref/validators/#how-validators-are-run



或仅限对于GenericIPAddressField使用验证器:

  from django.core.validators import ip_address_validators 
from django.core.exceptions import ValidationError

def save(self,* args,** kwargs):
try:
ip_address_validators('ipv4',self.ip_address)
除了ValidationError:
return
super(AccessPointIPAddress,self).save(* args,** kwargs)

它将使用以下验证器:

  ipv4_re = re.compile(r'^(25 [ 0-5] | 2 [0-4] \d |?。[0-1] \d \d)(\(25 [0-5] | 2 [0-4] \d | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | $ b  


Hi I have the following Django model

class AccessPointIPAddress(models.Model):
    '''Model for storing AccessPoint IP Addresses.'''
    ap = models.ForeignKey(AccessPoint, related_name='ip_addresses')
    ip_address = models.GenericIPAddressField(protocol='IPv4')
    datetime = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['datetime']
        get_latest_by = 'datetime'

And I am assuming that django's GenericIPAddressField does some string validation that a string is indeed a valid IP Address. I also read django's source and it does have some validation functions tied to GenericIPAddressField

But when I try to run this on django's shell:

# Assume that *ap* is a valid AccessPoint instance
# Notice ip_address IS NOT A VALID IP ADDRESS
>> AccessPointIPAddress.objects.create(ap=ap, ip_address='xxxxxx123123----')
<AccessPointIPAddress: ap xxxxxx123123---- 2015-05-18 12:39:25.491811>

I am expecting that it should raise some kind of ValueError or validation error since the given ip-address xxxxxx123123---- is not a valid ip address.

Am I missing something here? Or is this part of django broken? Currently using Django 1.6.5

解决方案

https://docs.djangoproject.com/en/1.8/ref/validators/#how-validators-are-run

See the form validation for more information on how validators are run in forms, and Validating objects for how they’re run in models. Note that validators will not be run automatically when you save a model, but if you are using a ModelForm, it will run your validators on any fields that are included in your form. See the ModelForm documentation for information on how model validation interacts with forms.

You can override save() method and do a full_clean() on the model instance as described in the docs here: https://docs.djangoproject.com/en/1.8/ref/validators/#how-validators-are-run

or only use validator for GenericIPAddressField:

from django.core.validators import ip_address_validators
from django.core.exceptions import ValidationError

def save(self, *args, **kwargs):
    try:
        ip_address_validators('ipv4', self.ip_address)
    except ValidationError:
        return
    super(AccessPointIPAddress, self).save(*args, **kwargs)

it will use the following validator:

ipv4_re = re.compile(r'^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$')
validate_ipv4_address = RegexValidator(ipv4_re, _('Enter a valid IPv4 address.'), 'invalid')

这篇关于Django GenericIPAddress字段未验证输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆