删除“添加另一个"在 Django 管理屏幕中

2022-01-25 00:00:00 python django django-admin

问题描述

每当我使用对象 B 的外键编辑对象 A 时,对象 B 的选项旁边都会出现一个加号选项添加另一个".如何删除该选项?

Whenever I'm editing object A with a foreign key to object B, a plus option "add another" is available next to the choices of object B. How do I remove that option?

我配置了一个无权添加对象 B 的用户.加号仍然可用,但是当我单击它时,它显示权限被拒绝".太丑了.

I configured a user without rights to add object B. The plus sign is still available, but when I click on it, it says "Permission denied". It's ugly.

我正在使用 Django 1.0.2

I'm using Django 1.0.2


解决方案

不推荐使用的答案

Django 让这成为可能.

DEPRECATED ANSWER

Django has since made this possible.

您是否考虑过使用 CSS 来简单地不显示按钮?也许这有点太老套了.

Have you considered instead, using CSS to simply not show the button? Maybe that's a little too hacky.

这是未经测试的,但我在想......

This is untested, but I'm thinking...

no-addanother-button.css

#_addanother { display: none }

admin.py

class YourAdmin(admin.ModelAdmin):
    # ...
    class Media:
        # edit this path to wherever
        css = { 'all' : ('css/no-addanother-button.css',) }

执行此操作的 Django 文档 -- 媒体作为静态定义

Django Doc for doing this -- Media as a static definition

注意/ 文档说这些文件将带有 MEDIA_URL,但在我的实验中它不是.您的里程可能会有所不同.

Note/ The documentation says the files will be prepended with the MEDIA_URL but in my experimentation it isn't. Your mileage may vary.

如果您发现这种情况适合您,可以快速解决此问题...

If you find this is the case for you, there's a quick fix for this...

class YourAdmin(admin.ModelAdmin):
    # ...
    class Media:
        from django.conf import settings
        media_url = getattr(settings, 'MEDIA_URL', '/media/')
        # edit this path to wherever
        css = { 'all' : (media_url+'css/no-addanother-button.css',) }

相关文章