2017-08-04 73 views
0

在我的DRF模型之前,如何拦截基地实地验证我有一个字段:自己的验证

myfield = ArrayField(
    models.CharField(max_length=7, blank=True, null=True), 
    blank=True, 
    null=True 
) 

和串行我正在写功能验证此MyField的:

def validate_myfield(self, value): # validate only for this myfield (value = field value from request) 
    **some validations** 

    return value 

但如果从基本字段的验证(从静止串行器)Expected a list of items but got type \"int\".:在请求字段myfield = "string"/integer_value或等我得到错误。

如何截获此错误MyField的验证之前(如果请求域不是字符串数组,验证不启动),并打印我的错误讯息?如果你想检查类type.isinstance的类型

回答

0

是非常有用的

def validate_myfield(self, value): # validate only for this myfield (value = field value from request) 
    assert isinstance(value,list) 
    # Your code here when the assertion pass 
    to excluse string,and it 
    assert not isinstance(value,(int,str)) 
    return value 
+0

A型误差函数之前,将出现启动 – John

+0

有点不正确描述的问题,编辑它 – John

相关问题