gpt4 book ai didi

python - Django - 在 Formset 中禁用对现有表单的编辑,但允许在新表单中进行编辑

转载 作者:行者123 更新时间:2023-11-28 17:13:29 25 4
gpt4 key购买 nike

当表单已经存在并且只需要更新几列时,如何禁用模型表单集中的表单字段,但在用户将新表单添加到表单集中时如何使这些相同的字段可编辑?

模型.py:

from django.db import models
from django.utils import timezone

class MyModel(models.Model):
idno = models.CharField(max_length=20)
date = models.DateTimeField(default=timezone.now)
entity = models.CharField(max_length=50)
logic = models.CharField(max_length=100)
choices = (
('1', 'Choice1'),
('2', 'Choice2'),
('3','Choice3'),
)
choices = models.CharField(
max_length=20,
choices=choices,
null=True,
)
comment = models.CharField(max_length=500, null=True)
def __str__(self):
return self.idno

表单.py:

from .models import MyModel
from django.forms import modelformset_factory, ModelForm

class MyForm(ModelForm):

class Meta:
model = MyModel
fields = '__all__'

def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.fields['idno'].disabled = True
self.fields['date'].disabled = True
self.fields['entity'].disabled = True
self.fields['logic'].disabled = True

MyFormSet = modelformset_factory(MyModel, extra=1, exclude=(), form=MyForm)

views.py:

from django.shortcuts import render
from django.http import HttpResponseRedirect
from .models import Alert
from .forms import AlertFormSet
from django.contrib import messages

def index(request):
newAlerts = MyModel.objects.filter(choices__isnull=True)
modelformset = MyFormSet(request.POST or None, queryset=newAlerts)
context = {'modelformset':modelformset}
if request.method == 'POST':
for form in modelformset:
if form.is_valid():
if form.has_changed():
form.save()
idno = form.cleaned_data['idno']
entity = form.cleaned_data['entity']
messages.success(request, 'idno %s for %s was saved' % (idno, entity))
return HttpResponseRedirect('/')
return render(request, 'selfserve/index.html', context)

index.html:

<form method="post" action="">
{% csrf_token %}
{{ modelformset.management_form }}
<div id="form_set">
{% for form in modelformset %}
<table class='no_error'>
{{ form.as_table }}
</table>
{% endfor %}
</div>
<input type="button" value="Add More" id="add_more">
<input type="submit" value="Submit">
<div id="empty_form" style="display:none">
<table class='no_error'>
{{ modelformset.empty_form.as_table }}
</table>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$('#add_more').click(function() {
var form_idx = $('#id_form-TOTAL_FORMS').val();
$('#form_set').append($('#empty_form').html().replace(/__prefix__/g, form_idx));
$('#id_form-TOTAL_FORMS').val(parseInt(form_idx) + 1);
});
</script>
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
</form>

现在,当用户使用“添加更多”按钮添加新表单时,我希望为现有表单禁用的字段也会被禁用。我看过这个问题:In a Django form, how do I make a field readonly (or disabled) so that it cannot be edited? .但是,我不明白如何组合解决方案的两个元素:django > 1.9 中的 fields.disabled = True,以及用于在表单集中划分现有表单和新表单的 if 语句。我还听说过 _construct_form 方法是一个潜在的选择,但我认为在模型表单创建中使用 if 语句会更清晰、更容易理解。

感谢您提供的所有见解!

最佳答案

在我看来,您可以在链接到的问题中使用该技术:

class MyForm(ModelForm):

class Meta:
model = MyModel
fields = '__all__'

def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
instance = getattr(self, 'instance', None)
if instance and instance.pk:
self.fields['idno'].disabled = True
self.fields['date'].disabled = True
self.fields['entity'].disabled = True
self.fields['logic'].disabled = True

请注意,您只能通过检查实例是否存在来执行此操作,因为所有字段都是必需的。如果其中一些不是,您将需要检查它们中的每一个是否都有值。

关于python - Django - 在 Formset 中禁用对现有表单的编辑,但允许在新表单中进行编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45799927/

25 4 0
文章推荐: python - Django 缺少 Vary :Cookie header for cached views
文章推荐: html - 如何在两列 Bootstrap 布局中向上移动一列?
文章推荐: css - 网站的自定义 CSS 编码/编码 block
文章推荐: javascript - 如何通过onclick将
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com