由于某种原因,我今天无法使用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
。我想知道它是否与外键或其他东西有关。
models.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中,最近添加了字段用户,日期时间和注释。现在,如果对于客户端,这些字段中的任何一个都没有值,即空白,我将收到错误Unable to serialize database
。
当我在mysql中查找用户,日期和注释时。客户端表显示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 | | +-----------+--------------+------+-----+---------+----------------+
答案 0 :(得分:5)
你需要告诉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
允许您以模型形式将字段留空
(例如,在管理员中)。(blank
和null
默认为False
)