且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用户的Django管理内联模型

更新时间:2023-12-01 13:16:04

你提到的似乎与你的代码中发生了什么有关,所以我并不确定这一点。但是,您在此处还有其他错误,因此潜在地修复这些错误也可以解决该错误。

The particular error you mention doesn't seem to have anything to do with what's going on in your code, so I'm not sure about that particularly. However, you have other errors here, so potentially fixing those will resolve that error as well.

首先,您需要指定 fk_name 您的 EmployerInline 。 Django在大多数情况下自动解析外键,但由于您有两个外键可以使用相同的型号,所以您必须给Django一些帮助。

First, you need to specify fk_name on your EmployerInline. Django resolves the foreign key automatically in most circumstances, but since you have two foreign keys to the same model, you have to give Django some help.

class EmployerInline(admin.TabularInline):
    model = Employer
    fk_name = 'create_user'

其次,您可能已经省略了它,但在注册之前必须先注销 User 。您还需要在注册时指定模型:

Second, you may have just omitted it, but you must unregister User before registering it. You also need to specify the model when registering:

admin.site.unregister(User)
admin.site.register(User, UserAdmin)