gpt4 book ai didi

mysql - 执行转储数据时出现 "Error: Unable to serialize database:"故障排除

转载 作者:可可西里 更新时间:2023-11-01 06:30:40 25 4
gpt4 key购买 nike

由于某些原因,今天我无法使用 python manage.py dumpdata 或从可以下载 mysql 文件的链接转储我的数据库。

我尝试使用 python manage.py dumpdata --traceback,这是我掌握的信息。

Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_manager(settings)
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/dumpdata.py", line 114, in handle
use_natural_keys=use_natural_keys)
File "/usr/local/lib/python2.7/site-packages/django/core/serializers/__init__.py", line 91, in serialize
s.serialize(queryset, **options)
File "/usr/local/lib/python2.7/site-packages/django/core/serializers/base.py", line 48, in serialize
self.handle_fk_field(obj, field)
File "/usr/local/lib/python2.7/site-packages/django/core/serializers/python.py", line 48, in handle_fk_field
related = getattr(obj, field.name)
File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 301, in __get__
raise self.field.rel.to.DoesNotExist
django.contrib.auth.models.DoesNotExist

它说 django.contrib.auth.models.DoesNotExist。我想知道它是否与外键有关。

模型.py

class Client(models.Model):
name = models.CharField(max_length = 40)
telephone = models.CharField(max_length = 20)
website = models.URLField(verify_exists = False)
fax = models.CharField(max_length = 20)
email = models.EmailField()
is_active = models.BooleanField()
user = models.ForeignKey(User)
datetime = models.DateTimeField(default=datetime.now)
note = models.TextField()
def __unicode__(self):
return self.name

从我的 models.py 中,字段 user、datetime 和 note 是最近添加的。现在,如果对于客户端,这些字段中的任何一个都没有值,即空白,我将收到错误 Unable to serialize database

当我在 mysql 中查找用户、日期时间和注释时。 Client 表显示 user_id、datetime 和 note 具有 Null 值(这是我想要的)。为什么它不允许 Null 值?

+-----------+--------------+------+-----+---------+----------------+| Field     | Type         | Null | Key | Default | Extra          |+-----------+--------------+------+-----+---------+----------------+| id        | int(11)      | NO   | PRI | NULL    | auto_increment | | name      | varchar(40)  | NO   |     | NULL    |                | | telephone | varchar(20)  | NO   |     | NULL    |                | | website   | varchar(200) | NO   |     | NULL    |                | | fax       | varchar(20)  | NO   |     | NULL    |                | | email     | varchar(75)  | NO   |     | NULL    |                | | is_active | tinyint(1)   | NO   |     | NULL    |                | | user_id   | int(11)      | YES  | MUL | NULL    |                | | datetime  | datetime     | YES  |     | NULL    |                | | note      | longtext     | YES  |     | NULL    |                | +-----------+--------------+------+-----+---------+----------------+

最佳答案

您需要告诉 Django 它应该允许在那些最近添加的字段上使用 NULL 值。它只查看字段定义,不从数据库中获取架构。

self.field.rel.to.DoesNotExist 错误最有可能在客户端没有关联用户且访问 client.user 时出现。这是因为默认情况下所有字段都是必需的。如果您如下所示更改模型定义,错误应该会消失。

class Client(models.Model):
# ...
user = models.ForeignKey(User,
null=True, blank=True)
datetime = models.DateTimeField(default=datetime.now,
null=True, blank=True)
note = models.TextField(null=True, blank=True)
  • null=True 分别允许字段上的 NULL None 值。
  • blank=True 允许您在模型表单中将字段留空(例如,在管理员中)。

(blanknull 默认都是 False)

关于mysql - 执行转储数据时出现 "Error: Unable to serialize database:"故障排除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6680283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com