0

我有一个使用 django-crispy 渲染的 modelform_formset。在使用的表单布局内,我有以下内容:

self.helper.layout = Layout(
    Field('remove', css_class="inline"),
    HTML('{{ form.instance.user.get_full_name|title }} ({{ form.instance.user }})'),
    Field('is_admin')
)

请注意{{form.instance}}- 这没有得到正确绑定。有没有办法获得特定模型的价值?

4

1 回答 1

0

I would recommend you do this. This captures the case where you don't have an instance :D

class XYXForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(XYXForm, self).__init__(*args, **kwargs)
        label = "New Object"
        if self.instance:
            label = '{0} {1}'.format(
                self.instance.user.get_full_name.capitalize(), 
                self.instance.user)
        self.helper = FormHelper()
        self.helper.form_id = 'community_form'
        self.helper.form_method = 'post'
        self.helper.layout = Layout(
            Field('remove', css_class="inline"),
            HTML(label),
            Field('is_admin')
        )

HTH

于 2013-05-26T23:17:33.930 回答