我创建了一个在Django用户模型上扩展的模型。我现在正在尝试使用这种类型的用户为数据库添加种子,但是在尝试使用loaddata
调用时出现错误。
我扩展了用户模型,创建了另一个名为FocalUser的用户类型。我用该信息创建了一个user.json文件。第一次出现错误时,我随后使用dumpdata
进行了仔细检查。该信息似乎不正确,或者像我从dump
所想象的那样。
这来自我创建FocalUser
的models.py文件:
class FocalUser(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
userID = CharField(max_length=50)
type_id = CharField(max_length=50)
is_active = BooleanField()
这是我的users.json文件:
[
{
"model": "focal_login.FocalUser",
"pk": 1,
"fields": {
"username": "kate@tamu.edu",
"email": "kate@tamu.edu",
"password": "password",
"first_name": "Kate",
"last_name": "Catalena",
"userID": 2,
"type_id": 2,
"is_active": "True"
}
}
]
由python3 manage.py loaddata users.json
引起的错误:
Traceback (most recent call last):
File "/Users/kate/.local/share/virtualenvs/login-E6JpMIQ_/lib/python3.5/site-packages/django/db/models/options.py", line 564, in get_field
return self.fields_map[field_name]
KeyError: 'last_name'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/kate/.local/share/virtualenvs/login-E6JpMIQ_/lib/python3.5/site-packages/django/core/serializers/json.py", line 69, in Deserializer
yield from PythonDeserializer(objects, **options)
File "/Users/kate/.local/share/virtualenvs/login-E6JpMIQ_/lib/python3.5/site-packages/django/core/serializers/python.py", line 116, in Deserializer
field = Model._meta.get_field(field_name)
File "/Users/kate/.local/share/virtualenvs/login-E6JpMIQ_/lib/python3.5/site-packages/django/db/models/options.py", line 566, in get_field
raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: FocalUser has no field named 'last_name'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/Users/kate/.local/share/virtualenvs/login-E6JpMIQ_/lib/python3.5/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/Users/kate/.local/share/virtualenvs/login-E6JpMIQ_/lib/python3.5/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/kate/.local/share/virtualenvs/login-E6JpMIQ_/lib/python3.5/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/kate/.local/share/virtualenvs/login-E6JpMIQ_/lib/python3.5/site-packages/django/core/management/base.py", line 353, in execute
output = self.handle(*args, **options)
File "/Users/kate/.local/share/virtualenvs/login-E6JpMIQ_/lib/python3.5/site-packages/django/core/management/commands/loaddata.py", line 72, in handle
self.loaddata(fixture_labels)
File "/Users/kate/.local/share/virtualenvs/login-E6JpMIQ_/lib/python3.5/site-packages/django/core/management/commands/loaddata.py", line 113, in loaddata
self.load_label(fixture_label)
File "/Users/kate/.local/share/virtualenvs/login-E6JpMIQ_/lib/python3.5/site-packages/django/core/management/commands/loaddata.py", line 168, in load_label
for obj in objects:
File "/Users/kate/.local/share/virtualenvs/login-E6JpMIQ_/lib/python3.5/site-packages/django/core/serializers/json.py", line 73, in Deserializer
raise DeserializationError() from exc
django.core.serializers.base.DeserializationError: Problem installing fixture '/Users/kate/Desktop/login/users.json':
dumpdata
看起来像“
[{"model": "contenttypes.contenttype", "pk": 1, "fields": {"app_label": "admin", "model": "logentry"}}, {"model": "contenttypes.contenttype", "pk": 2, "fields": {"app_label": "auth", "model": "permission"}}, {"model": "contenttypes.contenttype", "pk": 3, "fields": {"app_label": "auth", "model": "user"}}, {"model": "contenttypes.contenttype", "pk": 4, "fields": {"app_label": "auth", "model": "group"}}, {"model": "contenttypes.contenttype", "pk": 5, "fields": {"app_label": "contenttypes", "model": "contenttype"}}, {"model": "contenttypes.contenttype", "pk": 6, "fields": {"app_label": "sessions", "model": "session"}}, {"model": "contenttypes.contenttype", "pk": 7, "fields": {"app_label": "focal_login", "model": "focaluser"}}, {"model": "contenttypes.contenttype", "pk": 8, "fields": {"app_label": "focal_login", "model": "event"}}, {"model": "contenttypes.contenttype", "pk": 9, "fields": {"app_label": "focal_login", "model": "project"}}
我没有正确扩展用户模型来创建FocalUser吗?用户模型具有以下字段:用户名,电子邮件,密码,first_name和last_name。所以为什么当我尝试播种时出现错误,请说KeyError: 'last_name'
答案 0 :(得分:1)
您刚刚完成了默认Django用户模型与模型之间的OneToOne关系,以支持其他字段。
如果这适合您,则应将users.json更改为类似于以下内容的内容(更容易在db中创建User和FocalUser对象,然后将其转储以确保结构):
[
{
"model": "auth.user",
"pk": 1,
"fields": {
"username": "kate@tamu.edu",
"email": "kate@tamu.edu",
"password": "password",
"first_name": "Kate",
"last_name": "Catalena",
standard user fields
...
}
}
{
"model": "focal_login.FocalUser",
"pk": 1,
"fields": {
"user": 1,
"userID": 2,
"type_id": 2,
"is_active": "True"
}
}
]
但是我怀疑如果有substituting user documentation的情况下,您宁愿替换默认的Django用户模型