简体   繁体   English

Django - 列中的空值违反了 Django 管理中的非空约束

[英]Django - null value in column violates not-null constraint in Django Admin

I'm facing an Integrity Error in Django admin while trying to add data to the database.在尝试向数据库添加数据时,我在 Django 管理员中遇到了完整性错误。

The traceback is as follows:回溯如下:

  Environment:


  Request Method: POST
  Request URL: http://127.0.0.1:8000/site/admin/SilverInningsHelpline/classified/add/

  Django Version: 1.6.4
  Python Version: 2.7.3
  Installed Applications:
  ('django_admin_bootstrapped.bootstrap3',
   'django_admin_bootstrapped',
   'django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   'SilverInningsHelpline',
   'south')
  Installed Middleware:
  ('django.contrib.sessions.middleware.SessionMiddleware',
   'django.middleware.common.CommonMiddleware',
   'django.middleware.csrf.CsrfViewMiddleware',
   'django.contrib.auth.middleware.AuthenticationMiddleware',
   'django.contrib.messages.middleware.MessageMiddleware',
   'django.middleware.clickjacking.XFrameOptionsMiddleware')


  Traceback:
  File "/home/siddharth/SilverInnings/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
    114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/siddharth/SilverInnings/venv/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
    432.                 return self.admin_site.admin_view(view)(*args, **kwargs)
  File "/home/siddharth/SilverInnings/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
    99.                     response = view_func(request, *args, **kwargs)
  File "/home/siddharth/SilverInnings/venv/local/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
    52.         response = view_func(request, *args, **kwargs)
  File "/home/siddharth/SilverInnings/venv/local/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
    198.             return view(request, *args, **kwargs)
  File "/home/siddharth/SilverInnings/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
    29.             return bound_func(*args, **kwargs)
  File "/home/siddharth/SilverInnings/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
    99.                     response = view_func(request, *args, **kwargs)
  File "/home/siddharth/SilverInnings/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
    25.                 return func(self, *args2, **kwargs2)
  File "/home/siddharth/SilverInnings/venv/local/lib/python2.7/site-packages/django/db/transaction.py" in inner
    371.                 return func(*args, **kwargs)
  File "/home/siddharth/SilverInnings/venv/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in add_view
    1131.                 self.save_model(request, new_object, form, False)
  File "/home/siddharth/SilverInnings/venv/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in save_model
    860.         obj.save()
  File "/home/siddharth/SilverInnings/venv/local/lib/python2.7/site-packages/django/db/models/base.py" in save
    545.                        force_update=force_update, update_fields=update_fields)
  File "/home/siddharth/SilverInnings/venv/local/lib/python2.7/site-packages/django/db/models/base.py" in save_base
    573.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/home/siddharth/SilverInnings/venv/local/lib/python2.7/site-packages/django/db/models/base.py" in _save_table
    654.             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/home/siddharth/SilverInnings/venv/local/lib/python2.7/site-packages/django/db/models/base.py" in _do_insert
    687.                                using=using, raw=raw)
  File "/home/siddharth/SilverInnings/venv/local/lib/python2.7/site-packages/django/db/models/manager.py" in _insert
    232.         return insert_query(self.model, objs, fields, **kwargs)
  File "/home/siddharth/SilverInnings/venv/local/lib/python2.7/site-packages/django/db/models/query.py" in insert_query
    1511.     return query.get_compiler(using=using).execute_sql(return_id)
  File "/home/siddharth/SilverInnings/venv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
    903.             cursor.execute(sql, params)
  File "/home/siddharth/SilverInnings/venv/local/lib/python2.7/site-packages/django/db/backends/util.py" in execute
    69.             return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/siddharth/SilverInnings/venv/local/lib/python2.7/site-packages/django/db/backends/util.py" in execute
    53.                 return self.cursor.execute(sql, params)
  File "/home/siddharth/SilverInnings/venv/local/lib/python2.7/site-packages/django/db/utils.py" in __exit__
    99.                 six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/siddharth/SilverInnings/venv/local/lib/python2.7/site-packages/django/db/backends/util.py" in execute
    53.                 return self.cursor.execute(sql, params)

  Exception Type: IntegrityError at /site/admin/SilverInningsHelpline/classified/add/
  Exception Value: null value in column "category_id" violates not-null constraint

My models are as follows:我的模型如下:

class Categories(models.Model):
    id = models.AutoField(primary_key=True)
    type = models.CharField(max_length=300)

    def __unicode__(self):
        return self.type


class Subcategory(models.Model):
    id = models.AutoField(primary_key=True)
    parent = models.ForeignKey(Categories)
    name = models.CharField(max_length=300)

    def __unicode__(self):
        return self.name



class Classified(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=256)
    contact_person = models.CharField(max_length=300)
    email = models.CharField(max_length=100)
    address = models.ForeignKey(Address)
    subcategory = models.ForeignKey(Subcategory)
    phone_number = models.BigIntegerField(max_length=20, default=0)
    image = models.ImageField(blank=True, upload_to='dynamic/img/')
    NO = 'NO'
    YES = 'YES'
    APPROVAL = ((NO, 'no'), (YES, 'yes'))
    active = models.CharField(choices=APPROVAL, default=NO, max_length=3)
    verified = models.CharField(choices=APPROVAL, default=NO, max_length=3)

    def __unicode__(self):
        return self.name

The problems arise when I try to make an entry to the Classified table from Admin.当我尝试从 Admin 进入 Classified 表时出现问题。

Tried solution from here:从这里尝试解决方案:

IntegrityError: null value in column "city_id " violates not-null constraint as it was the closest to my problem. IntegrityError:“city_id”列中的空值违反了非空约束,因为它最接近我的问题。 My tables looked like this after trying the solution on that link:在该链接上尝试解决方案后,我的表格如下所示:

class Categories(models.Model):
    id = models.AutoField(primary_key=True)
    type = models.CharField(max_length=300, unique=True, default='All', null=True)

    def __unicode__(self):
        return self.type


class Subcategory(models.Model):
    id = models.AutoField(primary_key=True)
    parent = models.ForeignKey(Categories, null=True, blank=True, default='All')
    name = models.CharField(max_length=300)

    def __unicode__(self):
        return self.name



class Classified(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=256)
    contact_person = models.CharField(max_length=300)
    email = models.CharField(max_length=100)
    address = models.ForeignKey(Address)
    subcategory = models.ForeignKey(Subcategory)
    phone_number = models.BigIntegerField(max_length=20, default=0)
    image = models.ImageField(blank=True, upload_to='dynamic/img/')
    NO = 'NO'
    YES = 'YES'
    APPROVAL = ((NO, 'no'), (YES, 'yes'))
    active = models.CharField(choices=APPROVAL, default=NO, max_length=3)
    verified = models.CharField(choices=APPROVAL, default=NO, max_length=3)

    def __unicode__(self):
        return self.name

But this did not solve my problem and now I'm stuck.但这并没有解决我的问题,现在我被卡住了。

Looking at your models you shouldn't have field category_id in any of your tables.查看您的模型,您的任何表中都不应该有字段category_id Perhaps you changed your models but did not alter tables in the database.也许您更改了模型但没有更改数据库中的表。 Now when you create an entry, Django does not fill fields it doesn't know about of and this creates an error.现在当你创建一个条目时,Django 不会填充它不知道的字段,这会产生一个错误。 You should remove unneeded fields from your tables.您应该从表中删除不需要的字段。 Or if it is possible you can drop the whole database and run manage.py syncdb from scratch.或者,如果可能,您可以删除整个数据库并从头开始运行manage.py syncdb

只需在所有字段中添加null=True检查https://docs.djangoproject.com/en/3.1/topics/migrations/#postgresql

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django:“is_admin”列中的 null 值违反了非空约束 - Django: null value in column "is_admin" violates not-null constraint Django-“ imageLink”列中的空值违反了非空约束 - Django - Null value in column “imageLink” violates not-null constraint Django:关系的“id”列中的 null 值违反非空约束 - Django : null value in column "id" of relation violates not-null constraint “ user_id”列中的null值违反了非null约束Django - null value in column “user_id” violates not-null constraint Django “ author_id”列中的Django IntegrityError空值违反了非空约束 - Django IntegrityError null value in column “author_id” violates not-null constraint “电子邮件”列中的 null 值违反了 Django 中使用 Postgresql 的非空约束错误 - null value in column “email” violates not-null constraint error in Django using Postgresql “ user_id”列中的空值违反了非空约束Django形式 - null value in column “user_id” violates not-null constraint Django form “ marque_id”列中的空值违反了非空约束-Django 2.1.7 - Null value in column “marque_id” violates not-null constraint - Django 2.1.7 Django Rest Framework POST 失败:“cat_id”列中的空值违反了非空约束 - Django Rest Framework POST fails: null value in column "cat_id" violates not-null constraint 列“ publicatithon_date”中的错误为空值违反了Django中的非空约束 - Having error null value in column “publicatithon_date” violates not-null constraint in django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM