开发者

How to insert NULL in database using Django

I have a problem inserting NULL in Django. I mean i don't know how to do this.

I have function, lets call it find_position, then i have field in model like position (IntegerField, null=True, blank=True). This function inspect some html code, and if it match with some regex, it returns integer, but if it doesn't find - have to return NULL or None or something that I can put in database.

 def find_position(self, to_find, something): 
   ...
       if re.search(to_find, something开发者_JAVA技巧): 
           return i + (page * 10) + 1 
    return NULL # or what i have to return here?

And i have this:

_position = find_position(to_find, something)
p = Result(position=_position, ...)
r.save()

Previously i user CharField for position, and returned '-' if not find the result. But i had problems counting total results, like position__lte=10 (because it's not integer and it messed up with numbers and strings -.

What can I do with this?


Make the function return None (or not returning anything, it's the same as returning None), in database it will be saved Null:

 def find_position(self, to_find, something): 
   ...
       if re.search(to_find, something): 
           return i + (page * 10) + 1 
    return None
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜