Is it possible to hide the "Add another" link but keep the "Save and continue editing" button to have the possibility to add other lines ?
-
2Yes, it is possible. You need to customize the Django admin templates. Check this links: [How to override and extend django admin templates](http://stackoverflow.com/questions/6583877/how-to-override-and-extend-basic-django-admin-templates), [Django Admin: Custom template for a model](http://stackoverflow.com/questions/20092749/django-admin-custom-template-for-a-particular-model) – AlvaroAV Jan 28 '15 at 10:27
-
I have already customize the admin with a template named : base_site.html – Patrice Jan 28 '15 at 11:55
-
Wich is the path to this template ? It has to be 'templates/admin/base_site.html – AlvaroAV Jan 28 '15 at 12:18
-
It is another file named admin. The filepath: templates/material/admin/base_site.html. model.py, forms.py...are in material – Patrice Jan 28 '15 at 12:40
-
@andree Are you looking for `submit_line.html` ? – Raja Simon Jan 28 '15 at 12:43
-
yes, maybe my problem comes from submit_line.html – Patrice Jan 28 '15 at 13:12
3 Answers
Your question is not terribly specific, but there are ways to alter the behavior of the Admin without messing with the template.
In my case, I wanted to force the user to create the parent model first before adding objects to the inline. This was done like so:
class SomethingInline(admin.TabularInline):
model = Something
extra = 0
...
def get_max_num(self, request, obj=None, **kwargs):
max = super(SomethingInline, self).get_max_num(request, obj, **kwargs)
if obj:
return max
return 0
You can use something similar to limit max_num
dynamically. Once the inline reaches the max_num
number of lines, the "Add Another" button is disabled. Thus, if you dynamically set max_num
to +1 the number of current inline objects, the user can only add one at a time before saving.
In this example, you might do:
def get_max_num(self, request, obj=None, **kwargs):
if obj:
max = obj.something_set.count() + 1
else:
max = 1
return max

- 156
- 1
- 4
-
I have trouble..with : to limit max_num dynamically. what do you mean by dynamically set max_num to +1 ? – Patrice Jan 29 '15 at 09:42
-
You are defining the `get_max_num` method inside the Inline class, correct? – G Rice Jan 30 '15 at 17:08
-
I mean in my question How to hide in admin inlines the dynamic link "Add another" (It is not a button). In a css file input[name="_addanother"] { display: none; } don't work – Patrice Feb 05 '15 at 16:12
-
I understand that much. I am stating that Django can control whether or not that link is shown from the admin.py code, without the need to touch the templates or the site's CSS. This is done by adjusting the `max_num` attribute, detailed in the documentation here: https://docs.djangoproject.com/en/1.7/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin.max_num My answer also allows you to dynamically define the `max_num`, if you want. Once the number of lines in the Inline reaches `max_num`, the "Add Another" link will be removed from view automatically. – G Rice Feb 06 '15 at 17:02
The InlineModelAdmin.extra
, InlineModelAdmin.max_num
, InlineModelAdmin.min_num
and InlineModelAdmin.get_max_num()
.
https://docs.djangoproject.com/en/1.7/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin.max_num should be enough to get any behaviour you want.
Setting the max_num to 0
. Will disable adding new objects.
class ItemInline(admin.TabularInline):
model = Item
max_num = 0
Setting extra to 1 and max_num to 3. This will give one new line. And an add button that will disappear when 3 items are added.
class ItemInline(admin.TabularInline):
model = Item
extra = 1
max_num = 3
Play around with it to get the desired behaviour.

- 10,945
- 1
- 33
- 54
-
That not works. If extra=1, I have a new line. If extra=1 and max_num=3 or max_num=2....I have no new line and "add another" is hidden – Patrice Feb 07 '15 at 05:56
-
Like the other answers. It's not the point how to get it exactly right in your situation. These answers only show you how your problem can be solved. Ask better questions and receive better answers. So, what is the exact intended behaviour? – allcaps Feb 07 '15 at 10:45
-
I just want to have two blank lines (or three) without ever seeing the link "add another ". If I want to record these new lines after data entry, I click 'save' or 'save and continue editing' and two new blank lines (or three) are added. "add another" never appears. sorry if I was not clear – Patrice Feb 08 '15 at 04:55
-
No problem. Setting max_num and extra to the same value should do the trick. So both 2 or 3. As long as they are the same there shouldn't be an add another button. You are fine with 1.4. See https://docs.djangoproject.com/en/1.4/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin.extra – allcaps Feb 08 '15 at 09:01
-
I found the solution, with a css file in the media class : .inline-related tr.add-row a { display: none; } – Patrice Feb 13 '15 at 08:30
I found the solution, with a css file in the media class :
class Essai_TemperatureInline(admin.TabularInline):
model = Essai_Temperature
extra= 2
class Media:
css = { "all" : ("/static/css/java.css",) }
and the css file :
.inline-related tr.add-row a { display: none; }
Why all these complicated answers ?

- 209
- 1
- 4
- 15
-
@Patrice, because CSS will not protect you from malicious user. The form validation i.e. `get_max_num()` will. – dhill Apr 07 '16 at 11:23