Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何正确扩展django用户模型和种子数据库_Django_Database_Django Models_Sqlite_Dumpdata - Fatal编程技术网

如何正确扩展django用户模型和种子数据库

如何正确扩展django用户模型和种子数据库,django,database,django-models,sqlite,dumpdata,Django,Database,Django Models,Sqlite,Dumpdata,我创建了一个扩展到Django用户模型的模型。我现在正尝试使用这种类型的用户为数据库种子,但在尝试使用loaddata调用时出错 我扩展了用户模型,创建了一种称为FocalUser的不同用户类型。我用这些信息创建了一个user.json文件。第一次出现错误时,我使用dumpdata再次检查了这个错误。信息似乎不正确,或者正如我从转储文件中想象的那样 这是从models.py文件中创建的FocalUser: class FocalUser(models.Model): user = mod

我创建了一个扩展到Django用户模型的模型。我现在正尝试使用这种类型的用户为数据库种子,但在尝试使用
loaddata
调用时出错

我扩展了用户模型,创建了一种称为FocalUser的不同用户类型。我用这些信息创建了一个user.json文件。第一次出现错误时,我使用
dumpdata
再次检查了这个错误。信息似乎不正确,或者正如我从
转储文件中想象的那样

这是从models.py文件中创建的
FocalUser

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()
这是my 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': 

我没有正确地扩展用户模型来创建FocalUser吗?用户模型有以下字段:用户名、电子邮件、密码、名字和姓氏。那么,为什么我尝试设置种子时出现了一个错误,比如说
KeyError:'last_name'

您刚刚完成了默认Django用户模型和您的模型之间的OneToOne关系,以支持其他字段

如果这适合您,您应该将users.json更改为类似于以下内容的内容(更容易在db中创建User和FocalUser对象,然后将其转储以确定其结构):

但我怀疑,如果是这样的话,您宁愿替换默认的Django用户模型

[{"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"}}
[
  {
    "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" 
    }
  }

]