Opened 16 years ago

Closed 10 years ago

Last modified 8 years ago

#9071 closed New feature (worksforme)

Can't disable "add" popup links in the admin

Reported by: Jacob Owned by: Dario Ocles
Component: contrib.admin Version: 1.0
Severity: Normal Keywords: pyconuk
Cc: Ivan Virabyan, james@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: yes

Description

There's no way to prevent the little green "plus" icons from appearing on related fields in the admin (well, other than removing the related object from the admin). It should be possible to prevent those from appearing -- an admin option would make sense here.

Attachments (6)

add-r9017.diff (2.6 KB ) - added by Ivan Giuliani 16 years ago.
add-r9017-tests.diff (2.0 KB ) - added by Ivan Giuliani 16 years ago.
add-r9050.diff (2.5 KB ) - added by Ivan Giuliani 16 years ago.
add-r9751.diff (5.3 KB ) - added by jackymac 16 years ago.
updated patch to so it applies cleanly to SVN-9751, added docs
disable_add_r15922.diff (3.5 KB ) - added by Dario Ocles 14 years ago.
The last patch is obsolete. Updated the patch and adding test case.
disable_add_r15922_2011-05-05.diff (3.7 KB ) - added by Dario Ocles 14 years ago.

Download all attachments as: .zip

Change History (33)

comment:1 by Ivan Giuliani, 16 years ago

Owner: changed from nobody to Ivan Giuliani

by Ivan Giuliani, 16 years ago

Attachment: add-r9017.diff added

by Ivan Giuliani, 16 years ago

Attachment: add-r9017-tests.diff added

comment:2 by Ivan Giuliani, 16 years ago

Has patch: set

Attached patch adds an exclude_add option (I'm not happy with the naming, any suggestion is welcome) that allows to specify which related fields shouldn't have the add image. Fields should be specified in a list, like exclude_add = [ 'rel_field1', 'rel_field2' ].

However, docs are missing since I'm not a native english-speaker, so it would be nice if someone else could do that.

comment:3 by Adrian Holovaty, 16 years ago

Triage Stage: UnreviewedAccepted

I like the idea but haven't tested the patch (note there's a print statement in the patch).

by Ivan Giuliani, 16 years ago

Attachment: add-r9050.diff added

comment:4 by Ivan Giuliani, 16 years ago

Needs documentation: set

Woops, removed the print statement.

comment:5 by Malcolm Tredinnick, 16 years ago

What's the use-case here? If you have permission to add the object, it's giving you a way to add it. If you don't have permission to add objects of that type, it shouldn't be showing the plus-sign, but I believe that's already the case.

Saying that you can add it in one way, but not in another seems inconsistent.

in reply to:  5 comment:6 by fabionatali, 16 years ago

Replying to mtredinnick:

What's the use-case here? If you have permission to add the object, it's giving you a way to add it. If you don't have permission to add objects of that type, it shouldn't be showing the plus-sign, but I believe that's already the case.

Saying that you can add it in one way, but not in another seems inconsistent.

If you have a foreign key with a limit_choices_to filter, then in your admin you get a drop down menu with a selection of items. However, if you add a new item through the green plus link, you can actually circumvent this limit_choices_to filter. You can create a new object which doesn't agree with the limit_choices_to filter and it will be used in the foreign key field despite the filter.

This could be a scenario where disabling the green plus link would make sense.

My 2 cents.

comment:7 by vincent, 16 years ago

It seems that if you don't have the permission to add objects of a particular type, the add popup link is still there, and there's an ugly Permission Denied page if you click on it.

I tried with Group or User Permissions, and the problem is still there.

There's an old (3 years old) ticket opened here : http://code.djangoproject.com/ticket/1035

Another patch a little less old here : http://code.djangoproject.com/ticket/2927

But I found no patch for the current version of django.

comment:8 by milan612, 16 years ago

Hi,

I tried it with latest version of trunk SVN-9218, but doesnt work for me

Any suggestion ?

Milan

by jackymac, 16 years ago

Attachment: add-r9751.diff added

updated patch to so it applies cleanly to SVN-9751, added docs

comment:9 by jackymac, 16 years ago

Needs documentation: unset

I've updated the patch so that it now applies to SVN-9751. Also added some docs. The option is still called "exclude_add." I couldn't think of a better name for it. *shrugs*

I'd be awesome if this could be committed in time for 1.1. Anything I can do to help make that happen?

comment:10 by Chris Beaven, 15 years ago

Owner: changed from Ivan Giuliani to Chris Beaven
Status: newassigned

Slightly related, I just uploaded a new patch to #1035

comment:11 by Chris Beaven, 15 years ago

Owner: changed from Chris Beaven to Ivan Giuliani
Status: assignednew

(Sorry, didn't mean to accept the ticket)

comment:12 by Flo Ledermann, 15 years ago

Btw. you can remove the plus sign by using css - this will not really disable the feature but hide it if you shoot for a more consistent user experience:

#add_id_<<fieldname>> {
    display: none;
}

Best is to look up the id of the element in the admin pages' html source.

If you dont't have an admin css already, you can put this snippet in a file - say css/admin.css - and add a Media class to your Admin class:

    class Media:
        css = {
            'all': [settings.MEDIA_URL + 'css/admin.css']
        }

comment:13 by Dario Ocles, 14 years ago

Owner: changed from Ivan Giuliani to Dario Ocles
Status: newassigned

I took the idea and I made a new patch. I added a test case that I hope it would be correct and well-done.

by Dario Ocles, 14 years ago

Attachment: disable_add_r15922.diff added

The last patch is obsolete. Updated the patch and adding test case.

comment:14 by Luke Plant, 14 years ago

Severity: Normal
Type: New feature

comment:15 by patchhammer, 14 years ago

Easy pickings: unset
Patch needs improvement: set

disable_add_r15922.diff fails to apply cleanly on to trunk

by Dario Ocles, 14 years ago

comment:16 by Dario Ocles, 14 years ago

Patch needs improvement: unset

The patch had a little conflic, I updated the patch to apply to trunk.

comment:17 by Julien Phalip, 14 years ago

UI/UX: set

comment:18 by Chris Beaven, 13 years ago

Note that it's actually possible to do this now. Since r13708, the related admin widget has a can_add_related attribute.

Using the ModelAdmin.get_form hook, set this to False for the field(s) you want to disable the plus for.

comment:19 by Julien Phalip, 13 years ago

#17547 is a duplicate and has a patch.

comment:20 by atkinsonr@…, 13 years ago

At present this isn't possible in an InlineModelAdmin, however the patch in #17547 looks good as it is to BaseModelAdmin.

comment:21 by Ivan Virabyan, 12 years ago

Cc: Ivan Virabyan added

comment:22 by James Aylett, 11 years ago

Cc: james@… added

comment:23 by Tim Graham, 11 years ago

Patch needs improvement: set

Needs a patch that applies cleanly.

comment:24 by Roger Hunwicks, 10 years ago

For others that end up on this page as the result of a Google Search or similar, as described on http://stackoverflow.com/questions/10377642/how-to-use-can-add-related-in-django-admin, the full version of the "Using the ModelAdmin.get_form hook" comment is:

class ProductAdmin(ModelAdmin):

    def get_form(self, request, obj=None, **kwargs):
        """
        Don't allow adding new Product Categories
        """
        form = super(ProductAdmin, self).get_form(request, obj, **kwargs)
        form.base_fields['category'].widget.can_add_related = False
        return form

Given the simplicity of this, do we still need this ticket?

comment:25 by Claude Paroz, 10 years ago

Resolution: worksforme
Status: assignedclosed

Closing as per last comment and lack of activity.

comment:26 by Ryan Castner, 8 years ago

I know this was closed due to inactivity and the ease of doing this, but I feel like the solution, while working and simple is not obvious and requires knowledge of the internals of how the admin works to figure out.

A documented setting to attach to model admin like disable_add_related = ('field_name', ...) seems like it would be a better solution.

The purpose of this is for non-technical admins who should have the ability to add a user but would be confused by the ability to add new users inline while modifying a page, likely if you are creating a new model with a foreign key to 'user' for instance, you don't want to be creating a new user at that time, so displaying the option can only serve to confuse the user

comment:27 by Tim Graham, 8 years ago

Not every use case can have a ModelAdmin attribute. To keep complexity under control, I think it's better to avoid adding one where an alternative exists.

Note: See TracTickets for help on using tickets.
Back to Top