I'm having problems achieving a (probably) rather simple task. I have fully modifiable models (Prodotto, Comune) which are shown as "addable" fields, as shown in picture below. What I'd rather not see is the + (add) button for such fields, hence to remove their "addable" propriety in this form. I've tried setting has_add_permission=False within the two models, but it would make it impossibile to add new objects to such models completely, not only in this form.
How can I do that?
EDIT: To clarify my need, I'd like NOT to have the "+"s next to the fields of the FK models, but I do still want to be able to add whole new inlines. To be as clear as possible, as I wrote in a comment, considering a scenario such as this: https://code.djangoproject.com/attachment/ticket/20367/django_custom_user_admin_form.png I just need to have the "+"s next to Groups and Country removed.
EXISTING CODE:
models.py (of the specific application involved):
from django.db import models
from smart_selects.db_fields import ChainedForeignKey
from apps.comune.models import Comune, Cap
class Prodotto(models.Model):
SETTORE_CHOICES = (
('CAL', 'Accessori calzature'),
('ALI', 'Alimentari'),
('ARA', 'Arredamenti e accessori'),
('AEM', 'Auto e moto'),
('CAL', 'Calzature'),
('CEG', 'Cartaria e grafica'),
('CEP', 'Concerie e pelletterie'),
('EDI', 'Edilizia'),
('INV', 'Industrie varie'),
('IST', 'Istruzione'),
('MDC', 'Materiali da costruzione'),
('MMC', 'Metalmeccanica'),
('SEI', 'Serramenti e infissi'),
('STM', 'Strumenti musicali'),
('TEI', 'Terziario innovativo'),
('TAB', 'Tessile abbigliamento'),
('TCP', 'Trasporto cose e persone'),
('VAR', 'Vari'),
)
nome = models.CharField(max_length=100)
settore = models.CharField(max_length=40, choices=SETTORE_CHOICES)
class Meta:
verbose_name_plural = "prodotti"
verbose_name = "prodotto"
ordering = ['nome']
def __unicode__(self):
return self.nome.capitalize()
class Cliente(models.Model):
TIPOLOGIA_CHOICES = (
('AR', 'Artigiano'),
('CO', 'Commerciante'),
('GI', 'Grande impresa'),
('PI', 'Piccola impresa'),
)
FORMA_SOCIETARIA_CHOICES = (
('SNC', 'S.n.c.'),
('SRL', 'S.r.l.'),
('SPA', 'S.p.A.'),
('SAS', 'S.a.s.'),
('COOP', 'Coop.A.r.l.'),
('DI', 'D.I.'),
('SCARL', 'S.c.a.r.l.'),
('SCPA', 'S.c.p.a.'),
)
SETTORE_CHOICES = (
('CAL', 'Accessori calzature'),
('ALI', 'Alimentari'),
('ARA', 'Arredamenti e accessori'),
('AEM', 'Auto e moto'),
('CAL', 'Calzature'),
('CEG', 'Cartaria e grafica'),
('CEP', 'Concerie e pelletterie'),
('EDI', 'Edilizia'),
('INV', 'Industrie varie'),
('IST', 'Istruzione'),
('MDC', 'Materiali da costruzione'),
('MMC', 'Metalmeccanica'),
('SEI', 'Serramenti e infissi'),
('STM', 'Strumenti musicali'),
('TEI', 'Terziario innovativo'),
('TAB', 'Tessile abbigliamento'),
('TCP', 'Trasporto cose e persone'),
('VAR', 'Vari'),
)
ragione_sociale = models.CharField(max_length=200)
forma_societaria = models.CharField(
max_length=5, choices=FORMA_SOCIETARIA_CHOICES)
titolare = models.CharField(max_length=100, blank=True)
partita_iva = models.CharField(
max_length=11, verbose_name='Partita IVA', unique=True)
tipologia = models.CharField(max_length=2, choices=TIPOLOGIA_CHOICES)
settore = models.CharField(max_length=40, choices=SETTORE_CHOICES)
prodotto = models.ManyToManyField(Prodotto, blank=True)
class Meta:
verbose_name_plural = "clienti"
verbose_name = "cliente"
def __unicode__(self):
return self.ragione_sociale.capitalize()
class Sede(models.Model):
nome = models.CharField(max_length=100)
indirizzo = models.CharField(max_length=200, blank=True)
cliente = models.ForeignKey(Cliente)
comune = models.ForeignKey(Comune)
cap = ChainedForeignKey(
Cap,
chained_field="comune",
chained_model_field="comune",
show_all=False,
auto_choose=True,
)
class Meta:
verbose_name_plural = "sedi"
verbose_name = "sede"
ordering = ['nome']
def __unicode__(self):
return self.nome.capitalize() + ", " + self.indirizzo
admin.py (of the specific application involved):
from django.contrib import admin
from .models import Cliente, Prodotto, Sede
from apps.recapito.models import RecapitoCliente
class SedeInline(admin.TabularInline):
model = Sede
extra = 1
def provincia(self, obj):
return obj.comune.provincia
readonly_fields = ['provincia', ]
class RecapitoInline(admin.TabularInline):
model = RecapitoCliente
extra = 1
list_fields = ['cliente', 'tipo', 'recapito', ]
@admin.register(Cliente)
class ClienteAdmin(admin.ModelAdmin):
list_display = [
'ragione_sociale', 'forma_societaria', 'titolare', 'partita_iva', ]
list_filter = ['forma_societaria', ]
search_fields = ['ragione_sociale', ]
inlines = [RecapitoInline, SedeInline]
admin.site.register(Prodotto)
The admin interface of this app produces this:
Shortcut links 1 and 2 are the ones I need removed, being referred to columns (FKs) inside my inline classes. Shortcut links 3 and 4 are to be kept, since they refers to the inlines themselves.
To remove the "Add another" option, please add the below method in admin inline class.
def has_add_permission(self, request):
return False
Similarly if you want to disable "Delete?" option, add the following method in admin inline class.
def has_delete_permission(self, request, obj=None):
return False
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With