How to insert NULL in database using Django

送分小仙女□ 提交于 2020-01-13 07:02:20

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/6291102/how-to-insert-null-in-database-using-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!