How to insert NULL into a database using Django

I have a problem introducing NULL in Django. I mean, I donโ€™t know how to do this.

I have a function, let's call it find_position , then I have a field in the model of type position (IntegerField, null = True, blank = True). This function checks some html code, and if it matches some regular expression, it returns an integer, but if it does not find it, it should return NULL or None or something that I can put into the 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 was a CharField user for position and returned a '-' if I could not find the result. But I had problems calculating the final results, such as position__lte = 10 (because it is not an integer , and it messed up with numbers and strings - .

What can i do with this?

+1
source share
1 answer

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

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

All Articles