简体   繁体   English

Django:如何在内联模型表单集中默认情况下使字段不可编辑?

[英]Django: How do I make fields non-editable by default in an inline model formset?

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.如果用户单击该行上的“编辑”按钮,它将变为可编辑并且(使用 JavaScript)我会将原始小部件替换为可编辑的小部件。 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

I see that inlineformset_factory has an argument called formfield_callback .我看到inlineformset_factory有一个名为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>

This thread is a bit old, but for anyone looking:这个线程有点旧,但对于任何正在寻找的人:

in the form:形式:

myfield=forms.CharField( widget=forms.TextInput(attrs={'class':'disabled', 'readonly':'readonly'}))

The "readonly" is an HTML attribute that makes the form uneditable. “只读”是一个 HTML 属性,它使表单不可编辑。 "disabled" is a CSS class as you'll want to modify the default styling, also it makes the jQuery simpler. “禁用”是一个 CSS 类,因为您需要修改默认样式,它还使 jQuery 更简单。

To make readonly inputs editable when clicked, here's a jQuery example:要在单击时使只读输入可编辑,这是一个 jQuery 示例:

$('input.disabled').click(function(){
    $(this).removeAttr('readonly').removeClass('disabled');
});

I think you might be able to override the init function of your form that is used in a formset.我认为您可以覆盖表单集中使用的表单的 init 函数。 There you could check for initial_data, and dynamically build your forms like you're hoping to do.在那里您可以检查initial_data,并像您希望的那样动态构建表单。 At least, it sounds plausible in my head.至少,这在我的脑海中听起来似乎是合理的。

I had a question where I wanted to " Auto-generate form fields ", can found a solution for dynamically creating forms, it may help:我有一个问题,我想“ 自动生成表单字段”,可以找到动态创建表单的解决方案,它可能会有所帮助:

Auto-generate form fields for a Form in django 在 Django 中为表单自动生成表单字段

It's not clean and there's probably a better way to handle this.它不干净,可能有更好的方法来处理这个问题。

How about just sending the data as editable (normal formset) from django and do the value check with javascript, using javascript to toggle the widgets?如何从 django 将数据作为可编辑(正常表单集)发送并使用 javascript 进行值检查,使用 javascript 切换小部件?

form.instance.LastName 将显示值,form.initial.LastName 将显示主键。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 django 管理员中创建“仅创建”不可编辑字段 - How to make a “create-only” non-editable field in django admin 如何在模型类的Flask Admin视图中使字段不可编辑 - How to make a field non-editable in Flask Admin view of a model class 如何在 Django 中向自定义管理表单添加不可编辑的字段 - How do you add a non-editable field to a custom admin form in Django 如何在 Django 的内联表单集中编辑 model 对象 - How to edit model objects in an inline formset in Django 如何在Django Model Formset上设置额外字段的初始值 - How do I set initial values for extra fields on a Django Model Formset 如何让表单集继承表单的样式? 我的意思是,如何在 Django 中设置表单集的样式? - How do I make the formset inherit the styles of the form? I mean, how do I style the formset in Django? Django:无法为订单模型表单指定“已创建”,因为它是不可编辑的字段 - Django: 'created' cannot be specified for Order model form as it is a non-editable field django.core.exceptions.FieldError:不能为论坛 model 表单指定“日期”,因为它是不可编辑的字段 - django.core.exceptions.FieldError: 'date' cannot be specified for Forum model form as it is a non-editable field 如何在 QTableWidget 中使特定单元格可编辑而其余单元格不可编辑? - How to make particular cell editable and leave the rest non-editable in QTableWidget? Django 内联表单集不这样做 - Django inline formset does not do this
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM