Allow only editing the current selected Foreign Key in Django Admin

时间:2016-08-30 04:28:13

标签: python django django-admin

So currently I have something like this:

Model:

class ConfirmEmail(models.Model): 
     report = models.ForeignKey(Report)
     owner = models.CharField(max_length = 100)
     emails = models.ManyToManyField(Sdm)

Admin:

@admin.register(ConfirmEmail)
class ConfirmEmailAdmin(admin.ModelAdmin):
    change_form_template = 'admin/phone/index.html'
    readonly_fields = ('owner',)
    filter_horizontal = ('emails',)
    list_display = ('owner','report')

I create these objects in code - meaning I set the report object. But what I would like in the Django admin is if I could allow a user to edit that report object but only the one set. They would be allowed to change it (so hopefully the drop down menu would no longer be there) so the nice pencil icon would still be there, but things like that "+" icon would be gone.

And this is not to say the user can't edit all reports, it's just that in the ConfirmEmail Admin they can only view that specific report attached to it.

I've been smacking away at this and can't seem to get it work.

I would also be inclined to just have the current Report Form embedded into the ConfirmEmail form - but don't know how I would go about doing that.

1 个答案:

答案 0 :(得分:0)

您应首先为报表模型引入模型管理员,然后覆盖ReportAdmin的 has_add_permission 功能。

"fmw_domain"

你也可以删除/禁用页面中的+ strong按钮 ,但请注意,如果用户知道添加网址或禁用javascript,可能会造成损害。