How do I require an inline in the Django Admin?

前端 未结 6 1565
旧巷少年郎
旧巷少年郎 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:32

    You need to set min_num in inline and validate_min in formset.

    https://docs.djangoproject.com/en/1.8/topics/forms/formsets/#validate-min

    class SomeInline(admin.TabularInline):
        ...
        min_num = 1
    
        def get_formset(self, request, obj=None, **kwargs):
            formset = super().get_formset(request, obj=None, **kwargs)
            formset.validate_min = True
            return formset
    

提交回复
热议问题