【发布时间】:2011-09-11 13:50:28
【问题描述】:
我在 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?
我有这个:
_position = find_position(to_find, something)
p = Result(position=_position, ...)
r.save()
以前我为position 使用CharField,如果找不到结果则返回'-'。但是我在计算总结果时遇到了问题,例如 position__lte=10(因为它不是 integer 并且它与数字和字符串 - 混淆了。
我能用这个做什么?
【问题讨论】:
标签: database django django-models integer nullable