如何使用Django在数据库中插入NULL

Igo*_*gor 1 database django integer nullable django-models

我在Django中插入NULL时遇到问题.我的意思是我不知道该怎么做.

我有函数,让我们调用它find_position,然后我在模型中有字段position(IntegerField,null = True,blank = True).这个函数检查一些html代码,如果它与一些正则表达式匹配,它返回整数,但如果它没有找到 - 必须返回NULL或None或我可以放在数据库中的东西.

 def find_position(self, to_find, something): 
   ...
       if re.search(to_find, something): 
           return i + (page * 10) + 1 
    return NULL # or what i have to return here?
Run Code Online (Sandbox Code Playgroud)

我有这个:

_position = find_position(to_find, something)
p = Result(position=_position, ...)
r.save()
Run Code Online (Sandbox Code Playgroud)

以前我用CharField for position,如果找不到结果,则返回' - '.但我在计算总结果时遇到了问题,比如position__lte = 10(因为它不是,integer而且它与数字和字符串搞混了-.

我该怎么办?

man*_*nji 6

使函数返回None(或不返回任何内容,它与返回相同None),在数据库中将保存Null:

 def find_position(self, to_find, something): 
   ...
       if re.search(to_find, something): 
           return i + (page * 10) + 1 
    return None
Run Code Online (Sandbox Code Playgroud)