如何将空值存储为整数域 [英] How to store empty value as an Integerfield

查看:140
本文介绍了如何将空值存储为整数域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到所有类似的线程,阅读文档,并尝试了许多组合,以便在db中存储一个空值,因为$ code> IntegerField ,并且每次都失败。



我正在使用MySQL。



我的 models.py 定义了一个 age = models.IntegerField()字段。我从csv文件填充db,一些单元格没有价值。 Django文档说:

  Field.null 

如果为True,Django会将空值存储为NULL数据库。默认为False。
请注意,空字符串值将始终存储为空字符串,而不是NULL。

因为我正在使用 IntegerField 一个空字符串(csv中的一个空单元格)将被存储为db中的 NULL 。因此,我(想)必须将 null = True 添加到 age 字段。实际上,我已经尝试了更多:

  age = models.IntegerField()
age = models.IntegerField (null = True)
age = models.IntegerField(null = True,blank = True)
age = models.IntegerField(null = True,blank = True,default = None)

每当我将一个空字符串插入到一个数据库中,我得到



value $:$($)$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b

任何猜测我还能做什么?

解决方案

字符串不是整数;并且空白字符串不是 NULL 。您需要做的是捕获字段为空的那些实例,然后将其转换为

  foo =something#something来自您的CSV文件

try:
val = int(foo)
除了ValueError:
#foo是不能转换为
#一个数字的东西。它可以是一个空字符串,或
#字符串,如'hello'
#提供默认值
val =无

#现在使用val插入数据库
f = MyModel()
f.age = val
f.save()

blank 严格用于前端验证;它对数据库端没有任何影响:


请注意,这不同于 null null 纯粹是
数据库相关,而空白与验证相关。如果一个字段
具有 blank = True ,表单验证将允许输入一个空值。
如果一个字段有 blank = False ,该字段将被要求。


null 另一方面与数据库有关:


如果为True,Django将在数据库中将空值存储为NULL 。默认为False。


一个 IntegerField 需要可以转换为整数,所以当你传递一个空白字符串时,它不能转换它并引发异常。相反,如果您传递,并且您有 age = models.IntegerField(null = True),它知道以正确存储它。



总结:




  • age = models.IntegerField()



    字段是必需的,需要一个有效的整数值。它不会接受 None ,并且在数据库中不会有空值。有效值为-2147483648至2147483647


  • age = models.IntegerField(null = True) p>

    字段是必需的(表单验证)。如果该字段具有 None 作为值,那么它将被转换为数据库中的 NULL

    / li>
  • age = models.IntegerField(blank = True,null = True)



    不需要字段(表单验证)。如果该字段在中传递,则将被翻译为 NULL


  • age = models.IntegerField(blank = True)



    不需要字段(表单验证),但是有效的整数值需要传入,因为数据库不接受null。通常在这里,您将给它一个默认值,具有 default = 0 或在将值提交给orm之前完成一些验证。



I've seen all the similar threads, read the docs, and tried many combinations to store an empty value as IntegerField in db and failed every single time.

I am using MySQL.

My models.py defines an age=models.IntegerField() field. I populate db from csv file, and some cells have no value. Django docs says:

Field.null

If True, Django will store empty values as NULL in the database. Default is False.
Note that empty string values will always get stored as empty strings, not as NULL. 

Since I am working with IntegerField I want an empty string (an empty cell from the csv) to be stored as NULL in db. Therefore, I (think) have to add null=True to the age field. Actually, I've tried more than this:

age=models.IntegerField()
age=models.IntegerField(null=True)
age=models.IntegerField(null=True, blank=True)
age=models.IntegerField(null=True, blank=True, default=None)

and every time I am inserting an empty string to a db I got

ValueError: invalid literal for int() with base 10: ''

Any guess what else can I do?

解决方案

A string is not an integer; and a blank string is not None or NULL. What you need to do is catch those instances where the field is blank and then cast it to None.

foo = "something" # "something" is coming from your CSV file

try:
   val = int(foo)
except ValueError:
   # foo is something that cannot be converted to
   # a number. It could be an empty string, or a
   # string like 'hello'
   # provide a default value
   val = None

# Now use val to insert into the database
f = MyModel()
f.age = val
f.save()

blank is strictly for front end validation; it doesn't have any impact on the database side:

Note that this is different than null. null is purely database-related, whereas blank is validation-related. If a field has blank=True, form validation will allow entry of an empty value. If a field has blank=False, the field will be required.

null on the other hand, has to do with the database:

If True, Django will store empty values as NULL in the database. Default is False.

An IntegerField requires value that can be converted into an integer, so when you pass in a blank string, it cannot cast it and raises an exception. Instead, if you pass in None, and you have age = models.IntegerField(null=True), it knows to store it correctly.

To summarize:

  • age = models.IntegerField()

    Field is required and needs a valid integer value. It will not accept None and will have no null values in the database. Valid values are -2147483648 to 2147483647

  • age = models.IntegerField(null=True)

    Field is required (form validation). If the field has None as a value, it will be translated to NULL in the database.

  • age = models.IntegerField(blank=True, null=True)

    Field is not required (form validation). If the field is passed in None, it will be translated to NULL

  • age = models.IntegerField(blank=True)

    Field is not required (form validation), but a valid integer value needs to be passed in because the database does not accept null. Typically here you would give it a default value with default=0 or have some validation done before submitting the value to the orm.

这篇关于如何将空值存储为整数域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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