Skip to content

Django: disable inline option to add new referenced objects

The Django Web Framework makes it quite easy to add new referenced objects in the admin menu.

Let's say the model has two foreign keys in it:

class TeamMember(models.Model):
    team = models.ForeignKey(Team, on_delete=models.CASCADE, verbose_name=_("Team"))
    user = models.ForeignKey(CustomUser, on_delete=models.CASCADE, verbose_name=_("User"))

And the admin form:

class TeamMemberForm(forms.ModelForm):
    class Meta:
        model = TeamMember

class CustomTeamMemberAdmin(admin.ModelAdmin):
    form = TeamMemberForm
    add_form = TeamMemberForm
    model = TeamMember
    fieldsets = [
        (None,      {'fields': ['team', 'user']}),
    ]

admin.site.register(TeamMember, CustomTeamMemberAdmin)

Then Django will show small a small green "Django add reference" sign next to the fields:

However that is not always desirable. In my use case I want a clear separation between changing teams, and changing team members. Note: this can't be resolved with permissions (removing the permissions for one one model will remove the "add" option, but that's not what I want - I still want permissions for both, just separate the actions).

Django makes it easy to remove the option to add new references inline in this form. In order to do that, the "ModelAdmin" must be extended by a "get_form()" function:

class CustomTeamMemberAdmin(admin.ModelAdmin):
    form = TeamMemberForm
    add_form = TeamMemberForm
    model = TeamMember
    fieldsets = [
        (None,      {'fields': ['team', 'user']}),
    ]

    # disable green "+" buttons to add new objects in foreign table
    def get_form(self, request, obj=None, **kwargs):
        form = super(CustomTeamMemberAdmin, self).get_form(request, obj, **kwargs)
        form.base_fields['team'].widget.can_add_related = False
        form.base_fields['user'].widget.can_add_related = False
        return form

In this function, the widgets for the two fields have an extra "can_add_related" option set to "False". That is enough to disable this option.

By the way: the same way the "add new reference" option can be disabled, it is also possible to disable the "edit" and "delete" options:

  • widget.can_change_related will disable the "edit" button
  • widget.can_delete_related will disable the "delete" button
  • Twitter
  • Bookmark Django: disable inline option to add new referenced objects at del.icio.us
  • Facebook
  • Google Bookmarks
  • FriendFeed
  • Digg Django: disable inline option to add new referenced objects
  • Bloglines Django: disable inline option to add new referenced objects
  • Technorati Django: disable inline option to add new referenced objects
  • Fark this: Django: disable inline option to add new referenced objects
  • Bookmark Django: disable inline option to add new referenced objects at YahooMyWeb
  • Bookmark Django: disable inline option to add new referenced objects at Furl.net
  • Bookmark Django: disable inline option to add new referenced objects at reddit.com
  • Bookmark Django: disable inline option to add new referenced objects at blinklist.com
  • Bookmark Django: disable inline option to add new referenced objects at Spurl.net
  • Bookmark Django: disable inline option to add new referenced objects at Simpy.com
  • Bookmark Django: disable inline option to add new referenced objects at blogmarks
  • Bookmark Django: disable inline option to add new referenced objects with wists
  • wong it!
  • Bookmark using any bookmark manager!
  • Stumble It!
  • Identi.ca

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

No comments

Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
To leave a comment you must approve it via e-mail, which will be sent to your address after submission.
Form options