Django管理用户的内联模型

时间:2022-06-02 07:22:10

I have model as

我有模特儿

class Employer(models.Model):
    create_user = models.ForeignKey(User,unique=False,null=True, related_name='%(class)s_user_create')
    update_user = models.ForeignKey(User,unique=False,null=True, related_name='%(class)s_user_update')

and I would like to list all Employer objects while I was at details of user in Django admin panel.

我想在Django管理面板中查看用户的详细信息时列出所有Employer对象。

I have written something like

我写过类似的东西

admin.py

admin.py

class EmployerInline(admin.TabularInline):
    model = Employer

class UserAdmin(admin.ModelAdmin):
    inlines = [
               EmployerInline
    ]

admin.site.register(UserAdmin)

but it gives me error as 'MediaDefiningClass' object is not iterable

但它给了我错误,因为'MediaDefiningClass'对象不可迭代

How can I list employers that are created by a spesific user while I was looking for user's details ?

在查找用户详细信息时,如何列出由特定用户创建的雇主?

Thanks

谢谢

1 个解决方案

#1


11  

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.

您提到的特定错误似乎与您的代码中发生的事情没有任何关系,所以我对此不太确定。但是,您在此处有其他错误,因此可能会修复这些错误也会解决该错误。

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.

首先,您需要在EmployerInline上指定fk_name。 Django在大多数情况下自动解析外键,但由于你有两个外键到同一个模型,你必须给Django一些帮助。

class EmployerInline(admin.TabularInline):
    model = Employer
    fk_name = 'create_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)

#1


11  

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.

您提到的特定错误似乎与您的代码中发生的事情没有任何关系,所以我对此不太确定。但是,您在此处有其他错误,因此可能会修复这些错误也会解决该错误。

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.

首先,您需要在EmployerInline上指定fk_name。 Django在大多数情况下自动解析外键,但由于你有两个外键到同一个模型,你必须给Django一些帮助。

class EmployerInline(admin.TabularInline):
    model = Employer
    fk_name = 'create_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)