How to set default values in TabularInline formset in Django admin

前端 未结 3 1976
礼貌的吻别
礼貌的吻别 2021-02-05 22:58

How to set first default rows/values in django admin\'s inline?

    class Employee(models.Model):
        username = models.CharField(_(\'Username\'), max_length         


        
3条回答
  •  攒了一身酷
    2021-02-05 23:32

    from django.utils.functional import curry
    
    class DetailsInline(admin.TabularInline):
        model = Details
        formset = DetailsFormset
        extra = 3
    
        def get_formset(self, request, obj=None, **kwargs):
            initial = []
            if request.method == "GET":
                initial.append({
                    'label': 'first name',
                })
            formset = super(DetailsInline, self).get_formset(request, obj, **kwargs)
            formset.__init__ = curry(formset.__init__, initial=initial)
            return formset
    

    From here: Pre-populate an inline FormSet?

提交回复
热议问题