我正在用Django写我的第一个项目。我需要创建几种类型的具有不同字段的用户。我已经创建了与User具有1-1关系的Client类,并尝试为其创建管理表单
models.py
class Client(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete="CASCADE")
new_field = models.CharField("New field", max_length=30)
我正试图在django admin中创建表单,以使用内置的用户字段来管理客户端实例。
admin.py
class UserInline(admin.StackedInline):
model = User
class ClientAdmin(admin.ModelAdmin):
class Meta:
model = Client
fields = '__all__'
class ExtendedClientAdmin(ClientAdmin):
inlines = ClientAdmin.inlines + [UserInline]
admin.site.register(Client, ExtendedClientAdmin)
但是我遇到以下错误:
Performing system checks...
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x000001B23F18B0D0>
Traceback (most recent call last):
File "C:\work\test\vevn\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "C:\work\test\vevn\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "C:\work\test\vevn\lib\site-packages\django\core\management\base.py", line 425, in check
raise SystemCheckError(msg)
django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:
ERRORS:
<class 'backoffice.admin.UserInline'>: (admin.E202) 'auth.User' has no ForeignKey to 'backoffice.Client'.
System check identified 1 issue (0 silenced).
我应该如何为带有内联用户字段的客户端管理员创建表单?
答案 0 :(得分:0)
也许您首先要做:
python manage.py makemigrations
python manage.py migrate
为了将您的更改映射到数据库