Model的一列数据,default=None与null=True,如果没有null=True,则不允许该数据为None,相当于default=None不起作用

3人阅读 评论(0) 收藏 举报

# emos工单,投诉的内容
class Record(models.Model):
    jobid = models.CharField(max_length=50, default=None)
    customerserviceid = models.BigIntegerField()
 city = models.ForeignKey(City, on_delete=models.CASCADE, default=None, null=True)



Record.objects.create(customerserviceid=123, city=None)

可以看到报错最后一行写在不允许jobid为空,虽然有default=None,设置了默认值为空,但是不起作用。


Traceback (most recent call last):
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\MySQLdb\cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\MySQLdb\connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\MySQLdb\cursors.py", line 247, in execute
    res = self._query(query)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\MySQLdb\cursors.py", line 411, in _query
    rowcount = self._do_query(q)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query
    db.query(q)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\MySQLdb\connections.py", line 277, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1048, "Column 'jobid' cannot be null")
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\query.py", line 417, in create
    obj.save(force_insert=True, using=self.db)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\base.py", line 729, in save
    force_update=force_update, update_fields=update_fields)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\base.py", line 759, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\base.py", line 842, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\base.py", line 880, in _do_insert
    using=using, raw=raw)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\query.py", line 1125, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1281, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\backends\utils.py", line 100, in execute
    return super().execute(sql, params)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\backends\utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\backends\mysql\base.py", line 76, in execute
    raise utils.IntegrityError(*tuple(e.args))
django.db.utils.IntegrityError: (1048, "Column 'jobid' cannot be null")
查看评论

python None和Null

None 是什么对象?这个问题真的太抽象了 空对象? 是全局对象。。。。。。(type, int, str, list, dict, set……在面向对象编程的世界里:万物皆对象)>>> a = ...
  • dqchouyang
  • dqchouyang
  • 2017-07-14 17:20:32
  • 1057

Python中的NULL和None

与C不同的是,在python中是没有NULL的,取而代之的是None,它的含义是为空,但要注意和空列表与空字符串的区别,None的类型是Nonetype >>>a=None >>>type(a) 另...
  • songyunli1111
  • songyunli1111
  • 2017-07-15 14:40:46
  • 3942

python之false和None

None,  False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()都相当于False
  • u010378878
  • u010378878
  • 2016-03-29 21:47:31
  • 755

`default_nettype none有助于检查verilog错误

`default_nettype none  module test_default_nettype( input wire a1, input wire a2, output wire c1...
  • supenman_mwg
  • supenman_mwg
  • 2012-10-24 17:20:54
  • 3185

python代码`if not x:` 和`if x is not None:`和`if not x is None:`使用

代码中经常会有变量是否为None的判断,有三种主要的写法:  第一种是`if x is None`; 第二种是 `if not x:`;(非常容易出错的方式,一定不要养成这样写的习惯) 第三种是`if...
  • Sasoritattoo
  • Sasoritattoo
  • 2013-10-08 22:09:04
  • 99773

Python中的None与空字符(NULL)的区别

1.首先要了解Pythond的对象的概念:Python中,万物皆对象,所有的操作都是针对对象的,那什么是对象,5是一个int对象,‘oblong’是一个str对象,异常也是一个对象,抽象一点是,人,猫...
  • chenlei_525
  • chenlei_525
  • 2017-04-21 11:14:07
  • 9420

Android 中listView 滑动到顶部或底部时有阴影怎么办?android:fadingEdge="none"不管用?

用 android:overScrollMode="never" 可以去掉阴影
  • lyyuan01
  • lyyuan01
  • 2016-11-30 11:43:22
  • 657

【Scala】Scala中的Nothing,Null,None,Nil

NothingNothing是所有类型的子类,也是Null的子类。Nothing没有对象,但是可以用来定义类型。例如,如果一个方法抛出异常,则异常的返回值类型就是Nothing(虽然不会返回) 。 ...
  • JasonDing1354
  • JasonDing1354
  • 2015-07-07 14:12:04
  • 1879

Scala中的None,Nothing,Null,Nil

在scala中这四个类型名称很类似,作用确实完全不同的。  None是一个object,是Option的子类型,定义如下  case object None extends Option[No...
  • bluejoe2000
  • bluejoe2000
  • 2014-06-13 10:57:47
  • 16637

python 中的None 和Null

None 指的的对象 Null 指的的类型 详细理解后再备注
  • u014221266
  • u014221266
  • 2016-12-07 10:50:43
  • 994
    个人资料
    持之以恒
    等级:
    访问量: 6280
    积分: 686
    排名: 8万+
    文章分类
    最新评论