How do I make a TabularInline required in Django admin? -
i want tabularinline field in django admin required. how proceed? here's code:
admin.py
class schoolinline(tabularinline): model = school.labs.through = 1 class laboratoryadmin(modeladmin): inlines = [schoolinline] register(lab, laboratoryadmin)
i simplified lot problem, that's it. in result drop-down list schools. problem field isn't required, want required. how can simple way?
forms.py
# 1 form required django.core.exceptions import validationerror django.forms.models import baseinlineformset class atleastoneformset(baseinlineformset): def clean(self): super(atleastoneformset, self).clean() non_empty_forms = 0 form in self: if form.cleaned_data: non_empty_forms += 1 if non_empty_forms - len(self.deleted_forms) < 1: raise validationerror("please fill @ least 1 form.")
forms.py
# first form not empty , can not deleted django.forms.models import baseinlineformset class requiredinlineformset(baseinlineformset): def _construct_form(self, i, **kwargs): form = super(requiredinlineformset, self)._construct_form(i, **kwargs) if < 1: form.empty_permitted = false return form
you need change view , remove delete
button first form shown here: https://docs.djangoproject.com/en/dev/topics/forms/formsets/#manually-rendered-can-delete-and-can-order
admin.py
from django.contrib.admin import tabularinline class schoolinline(tabularinline): model = school.labs.through = 1 formset = requiredinlineformset # or atleastoneformset
Comments
Post a Comment