Different initial data for each form in a Django formset

前端 未结 4 1267
灰色年华
灰色年华 2020-12-07 17:37

Is it possible to prepopulate a formset with different data for each row? I\'d like to put some information in hidden fields from a previous view.

According to the d

4条回答
  •  一整个雨季
    2020-12-07 18:18

    I had this problem and I made a new widget:

    from django.forms.widgets import Select
    from django.utils.safestring import mark_safe
    class PrepolutatedSelect(Select):
        def render(self, name, value, attrs=None, choices=()):
            if value is None: value = ''
            if value == '':
                value = int(name.split('-')[1])+1
            final_attrs = self.build_attrs(attrs, name=name)
            output = [u'' % flatatt(final_attrs)]
            options = self.render_options(choices, [value])
            if options:
                output.append(options)
            output.append('')
            return mark_safe(u'\n'.join(output))
    

    Maybe this will work for you too.

提交回复
热议问题