Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Last inline not displayed, if has_add_permission returns False #132

Open
misli opened this issue Nov 29, 2018 · 4 comments
Open

Last inline not displayed, if has_add_permission returns False #132

misli opened this issue Nov 29, 2018 · 4 comments

Comments

@misli
Copy link

misli commented Nov 29, 2018

Lets have following simple test application:
models.py:

from django.db import models

class Parent(models.Model):
    name = models.CharField(max_length=20)

class Child(models.Model):
    parent = models.ForeignKey(Parent, on_delete=models.CASCADE)
    name = models.CharField(max_length=20)

admin.py:

from django.contrib import admin

from .models import Child, Parent


class ChildInlineAdmin1(admin.TabularInline):
    model = Child

    def has_add_permission(self, request):
        return False


class ChildInlineAdmin2(admin.TabularInline):
    model = Child


class ParentAdmin(admin.ModelAdmin):
    inlines = (ChildInlineAdmin1, ChildInlineAdmin2)


admin.site.register(Parent, ParentAdmin)

The first Inline admin doesn't show the last child.

@misli
Copy link
Author

misli commented Nov 29, 2018

screenshot

@misli
Copy link
Author

misli commented Nov 29, 2018

I've found that it only happens with Django 2. Following patch works for me, but it is obviously not backwards compatible:

--- bootstrap_admin/templates/admin/edit_inline/tabular.html.orig    2018-11-29 20:54:55.766817156 +0100
+++ bootstrap_admin/templates/admin/edit_inline/tabular.html 2018-11-29 21:11:30.891857420 +0100
@@ -29,7 +29,7 @@
             {% if inline_admin_form.form.non_field_errors %}
               <tr><td colspan="{{ inline_admin_form|cell_count }}">{{ inline_admin_form.form.non_field_errors }}</td></tr>
             {% endif %}
-            <tr class="form-row {% cycle "row1" "row2" %} {% if inline_admin_form.original or inline_admin_form.show_url %}has_original{% endif %}{% if forloop.last %} empty-form{% endif %}"
+            <tr class="form-row {% cycle "row1" "row2" %} {% if inline_admin_form.original or inline_admin_form.show_url %}has_original{% endif %}{% if forloop.last and inline_admin_formset.has_add_permission %} empty-form{% endif %}"
                  id="{{ inline_admin_formset.formset.prefix }}-{% if not forloop.last %}{{ forloop.counter0 }}{% else %}empty{% endif %}">
             <td class="original">
               {% if inline_admin_form.original or inline_admin_form.show_url %}<p>

@jasonbodily
Copy link

jasonbodily commented Aug 6, 2020

Same concerns here.

from django.contrib import admin
from app.models import Tags

class TagsInline(admin.TabularInline):
    model = Tags
    fields = ('created_at', 'updated_at')
    extra = 0

    def has_add_permission(self, request, obj=None):
        return False

    def has_delete_permission(self, request, obj=None):
        return False

I would expect that returning False on the has_add_permission method would only remove the ability to create instances. Instead it also hides entries of the queryset. When I remove bootstrap-admin from my projects dependencies, those query entries show. Also, this behavior does not occur in Django 1.11 with admin-bootstrap 0.3.9

Environment:

  • Python 3.6.3
  • Django 2.2
  • admin-bootstrap 0.4.4

Thanks for your work on this repo

@Megajoe17
Copy link

Adding condition "and inline_admin_formset.has_add_permission" to template is fixing problem:

/templates/admin/edit_inline/tabular.html

<tr class="form-row {% cycle "row1" "row2" %} {% if inline_admin_form.original or inline_admin_form.show_url %}has_original{% endif %}{% if forloop.last and inline_admin_formset.has_add_permission %} empty-form{% endif %}"

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants