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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -