Django:如何使内嵌模式窗体中的默认字段不可编辑? [英] Django: How do I make fields non-editable by default in an inline model formset?

查看:254
本文介绍了Django:如何使内嵌模式窗体中的默认字段不可编辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个内联模型表单,如果这些字段在页面加载时已经有值,我想使字段不可编辑。如果用户点击该行上的编辑按钮,它将变为可编辑的(使用JavaScript),我将用可编辑的部件替换原始的小部件。加载页面时,我想做这样的事情:

I have an inline model formset, and I'd like to make fields non-editable if those fields already have values when the page is loaded. If the user clicks an "Edit" button on that row, it would become editable and (using JavaScript) I would replace the original widgets with editable ones. I'd like to do something like this when loading the page:

for field in form.fields:
    if field.value:
        # display as text
    else:
        # display as my standard editable widget for this field

我看到 inlineformset_factory 有一个名为 formfield_callback 的参数。我怀疑这可能是有用的,但是因为我没有找到任何文件。任何人都可以指出一些有用的文档,如何帮助我解决这个问题?

I see that inlineformset_factory has an argument called formfield_callback. I suspect that this could be useful, but so for I haven't found any documentation for it. Can anyone point me to some useful documentation for this, and how it can help me solve this problem?

推荐答案

还有一点希望这是你正在寻找的。

This one stumped me for a bit too. Hopefully this is what you're looking for.

<TABLE>
    <form method="post" action=".">
        {{ formset.management_form }}
        {% for form in formset.forms %}
            {{ form.id }}
            <tr>
                <td>{{ form.FirstName }}</td> <!-- This is a normal, editable field -->
                <td>{{ form.instance.LastName }}</td> <!-- 'instance' is your actual Django model. LastName displays the text from the last name field -->
            </tr>
        {% endfor %}
    </form>
</TABLE>

这篇关于Django:如何使内嵌模式窗体中的默认字段不可编辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆