Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I require an inline in the Django Admin?

I have the following admin setup so that I can add/edit a user and their profile at the same time.

class ProfileInline(admin.StackedInline):     """     Allows profile to be added when creating user     """     model = Profile   class UserProfileAdmin(admin.ModelAdmin):     """     Options for the admin interface     """     inlines = [ProfileInline]     list_display = ['edit_obj', 'name', 'username', 'email', 'is_active',         'last_login', 'delete_obj']     list_display_links = ['username']     list_filter = ['is_active']     fieldsets = (         (None, {             'fields': ('first_name', 'last_name', 'email', 'username',                 'is_active', 'is_superuser')}),         )     ordering = ['last_name', 'first_name']     search_fields = ['first_name', 'last_name']  admin.site.register(User, UserProfileAdmin) 

The problem is I need two of the fields in the Profile inline form to be required when adding the user. The inline form doesn't validate unless input is entered. Is there anyway to make the inline required, so that it can't be left blank?

like image 628
AJ. Avatar asked Jul 30 '09 14:07

AJ.


People also ask

What is inline formset in django?

Django formset allows you to edit a collection of the same forms on the same page. It basically allows you to bulk edit a collection of objects at the same time.

How do I restrict access to admin pages in django?

Django admin allows access to users marked as is_staff=True . To disable a user from being able to access the admin, you should set is_staff=False . This holds true even if the user is a superuser. is_superuser=True .

What we can do in admin portal in django?

Overview. The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.


2 Answers

I took Carl's advice and made a much better implementation then the hack-ish one I mentioned in my comment to his answer. Here is my solution:

From my forms.py:

from django.forms.models import BaseInlineFormSet   class RequiredInlineFormSet(BaseInlineFormSet):     """     Generates an inline formset that is required     """      def _construct_form(self, i, **kwargs):         """         Override the method to change the form attribute empty_permitted         """         form = super(RequiredInlineFormSet, self)._construct_form(i, **kwargs)         form.empty_permitted = False         return form 

And the admin.py

class ProfileInline(admin.StackedInline):     """     Allows profile to be added when creating user     """     model = Profile     extra = 1     max_num = 1     formset = RequiredInlineFormSet   class UserProfileAdmin(admin.ModelAdmin):     """     Options for the admin interface     """     inlines = [ProfileInline]     list_display = ['edit_obj', 'name', 'username', 'email', 'is_active',         'last_login', 'delete_obj']     list_display_links = ['username']     list_filter = ['is_active']     fieldsets = (         (None, {             'fields': ('first_name', 'last_name', 'email', 'username',                 'is_active', 'is_superuser')}),         (('Groups'), {'fields': ('groups', )}),     )     ordering = ['last_name', 'first_name']     search_fields = ['first_name', 'last_name']   admin.site.register(User, UserProfileAdmin) 

This does exactly what I want, it makes the Profile inline formset validate. So since there are required fields in the profile form it will validate and fail if the required information isn't entered on the inline form.

like image 81
AJ. Avatar answered Sep 17 '22 19:09

AJ.


Now with Django 1.7 you can use parameter min_num. You do not need class RequiredInlineFormSet anymore.

See https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin.min_num

class ProfileInline(admin.StackedInline):     """     Allows profile to be added when creating user     """     model = Profile     extra = 1     max_num = 1     min_num = 1 # new in Django 1.7   class UserProfileAdmin(admin.ModelAdmin):     """     Options for the admin interface     """     inlines = [ProfileInline]     ...   admin.site.register(User, UserProfileAdmin) 
like image 23
quick Avatar answered Sep 21 '22 19:09

quick