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

How to properly extend django user model and seed databse

我创建了一个扩展 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'

您刚刚完成了默认 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 用户模型