Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Removing 'Add' link in Django admin sidebar

The Django admin index page lists models in a left side sidebar, each with an ‘Add’ link to instantiate a new object. Model list

I want to remove that ‘Add’ link for just one of the models (Appointments) in the side bar, while retaining it within inline views.

First try: set add_url to false.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

# admin.py
class AppointmentAdmin(admin.ModelAdmin):
    add_url = False

This did not work. The ‘Add’ link still appears. The docs say add_url is an AdminSite method. I didn’t want to apply this site-wide, but worth a try:

Second try:

#admin.py
class AppointmentSitewideAdmin(admin.AdminSite):
    add_url = None

Didn’t work. Duh, ‘AppointmentSitewideAdmin’ refers to nothing.

Third try: override the app_list.html template. This works, but seems like the brute force way. There must be something more elegant? I’ve looked at defining-a-custom-app-list-in-django-admin-index-page, more complex, not directly applicable, and uses AdminSite (I’m assuming I need to be working local to this app, i.e. admin.ModelAdmin).

{% if app_list %}
  {% for app in app_list %}
    <div class="app-{{ app.app_label }} module{% if app.app_url in request.path|urlencode %} current-app{% endif %}">
      <table>
        <caption>
          <a href="{{ app.app_url }}" class="section" title="{% blocktranslate with name=app.name %}Models in the {{ name }} application{% endblocktranslate %}">{{ app.name }}</a>
        </caption>
        {% for model in app.models %}
            <tr class="model-{{ model.object_name|lower }}{% if model.admin_url in request.path|urlencode %} current-model{% endif %}">
              {% if model.admin_url %}
                <th scope="row"><a href="{{ model.admin_url }}"{% if model.admin_url in request.path|urlencode %} aria-current="page"{% endif %}>{{ model.name }}</a></th>
              {% else %}
                <th scope="row">{{ model.name }}</th>
              {% endif %}

              {# --------------- THE RELEVANT LINE ------------------- #}
              {% if model.add_url and model.name != 'Appointments' %}
                <td><a href="{{ model.add_url }}" class="addlink">{% translate 'Add' %}</a></td>
              {% else %}
                <td></td>
              {% endif %}

              {% if model.admin_url and show_changelinks %}
                {% if model.view_only %}
                  <td><a href="{{ model.admin_url }}" class="viewlink">{% translate 'View' %}</a></td>
                {% else %}
                  <td><a href="{{ model.admin_url }}" class="changelink">{% translate 'Change' %}</a></td>
                {% endif %}
              {% elif show_changelinks %}
                <td></td>
              {% endif %}
            </tr>
        {% endfor %}
      </table>
    </div>
  {% endfor %}
{% else %}
  <p>{% translate 'You don’t have permission to view or edit anything.' %}</p>
{% endif %}

>Solution :

Add button is controlled by add permissions. You can hide it by using:

class AppointmentAdmin(admin.ModelAdmin):
    def has_add_permission(self, request):
        return False
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading