如何使用Django在数据库中插入NULL [英] How to insert NULL in database using Django

查看:435
本文介绍了如何使用Django在数据库中插入NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Django中插入NULL时出现问题。我的意思是我不知道该怎么做。

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

我有函数,让它调用 find_position 我有模型中的字段像 position (IntegerField,null = True,blank = True)。这个函数检查一些html代码,如果它匹配一些regex,它返回整数,但如果它不找到 - 必须返回NULL或无或我可以放在数据库中的东西。

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): 
           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()

以前,我使用CharField for position ,如果没有找到结果,但我有问题计数总结果,如position__lte = 10(因为它不是整数,它搞砸了数字和字符串 -

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 -.

我能做些什么?

推荐答案

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

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

这篇关于如何使用Django在数据库中插入NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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