Django - 禁止在Formset中编辑现有表单但允许以新表单编辑

时间:2017-08-21 14:40:58

标签: python django forms python-3.x

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

models.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

forms.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中的fields.disabled = True&gt; 1.9,以及用于描述formset中现有表单和新表单之间的if语句。我也听说_construct_form方法是一个潜在的选择,但我认为模型表创建中的if语句更清晰,更容易理解。

感谢您提供的所有见解!

1 个答案:

答案 0 :(得分:1)

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

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

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