How do I require an inline in the Django Admin?

前端 未结 6 1560
旧巷少年郎
旧巷少年郎 2020-12-13 09:44

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):
    \"\"\"
             


        
6条回答
  •  情歌与酒
    2020-12-13 10:21

    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)
    

提交回复
热议问题