隐藏django模型中的字段

时间:2011-06-13 12:42:15

标签: django hidden-field modelform

django文档建议我可以在模板中识别隐藏/可见字段。我有两个模型,AddressInfo和PharmacyInfo。我需要从一个页面输入两个表的数据。我想隐藏address_id字段(来自PharmacyInfo模型)。我目前有:

    {% for form in formset %}
    {% if form.non_field_errors %}
        <div class="form_errors">
            {{ form.non_field_errors }}
        </div>
    {% endif %}

    {% for field in form.visible_fields%}
        <div class="field_content">                  
                <label>{{field.label_tag }}</label>
        <div class="field">

            {% if field.help_text %}
            <div class = "help_text">
                {{ field.help_text }}
            </div>
            {% endif %}               
                {{ field }}
                {{ field.errors }}
        </div> 
        </div>
    {% endfor %}
{% endfor %}



class PharmForm(ModelForm):
    class Meta:
        model = PharmInfo
        widgets = {
            'address_id': forms.HiddenInput()
        }



class AddressForm(ModelForm):
    class Meta:
        model = AddressInfo



class AddressInfo(models.Model):
    address_id = models.AutoField(primary_key=True)

    address_1 = models.CharField("Address Line 1", max_length = 64)
    address_2 = models.CharField("Address Line 2", max_length = 64, blank=True, null=True)
    address_3 = models.CharField("Address Line 3", max_length = 64, blank=True, null=True)
    address_4 = models.CharField("Address Line 4", max_length = 64, blank=True, null=True)
    town_city = models.CharField("Town or City", max_length = 32)
    post_code = models.CharField("Post Code", max_length = 8)
    phone = models.CharField("Phone Number - numbers 0-9 only", 
                        max_length = 16) #this must be numeric 0-9 only for auto-dial functionality.
    email = models.CharField("email address", max_length = 64) #must be valid email



class PharmInfo(models.Model):
    pharm_id = models.AutoField(primary_key=True)
    pharm_name = models.CharField("Pharamcy Name", max_length = 64)
    address_id = models.ForeignKey(AddressInfo, db_column="address_id")



def pharmView (request, id=None):
    pharmForm = PharmForm()
    addForm = AddressForm()
    if request.method == 'POST': #this is a form submission
        if id == None: #it's a new record
            pharmForm = PharmForm(request.POST)
            addForm = AddressForm(request.POST)
            if addForm.is_valid(): #add the address_id into the pharmForm.
                request.POST = request.POST.copy()
                request.POST['address_id'] = addForm.save().pk
                pharmForm = PharmForm(request.POST)
                if pharmForm.is_valid(): 
                    pharm = pharmForm.save()
                    return render_to_response('thanks.html', 
                        {'form1': pharmForm,
                         'form2': addForm},
                        context_instance=RequestContext(request),
                    )

        else: #it's an existing record
            pharm = PharmInfo.objects.get(pk=id)
            address = pharm.address_id
            pharmForm = PharmForm(request.POST, instance = pharm)
            addForm = AddressForm(request.POST, instance = address)
            if pharmForm.is_valid() and addForm.is_valid():
                pharmForm.save()
                addForm.save()
                return render_to_response('updateThanks.html',
                    {'form1': pharmForm,
                     'form2': addForm},
                    context_instance=RequestContext(request),
                )
    else: 
        if id != None: #form bound to a pharmacy record
            pharm = PharmInfo.objects.get(pk=id)
            address = pharm.address_id
            pharmForm = PharmForm(instance = pharm)
            addForm = AddressForm(instance = address)
            return render_to_response('institutions/pharm.html', 
                {'form1': pharmForm,
                 'form2': addForm},
                context_instance=RequestContext(request),
                )            

    return render_to_response('institutions/pharm.html', 
            {'form1': pharmForm,
             'form2': addForm},
            context_instance = RequestContext(request),
        )

此代码隐藏了address_id字段的输入框,但仍显示标签。我想隐藏整个div,但'form.visible_fields'不会将其从输出中排除。这让我疯了。任何人都可以告诉我如何以模板可见的方式标记address_id字段。

2 个答案:

答案 0 :(得分:19)

如果您不打算在表单中的所有上编辑address_id字段,则应使用Using a subset of fields in the form中记录的exclude元字段}}

结果如下:

class PharmForm(ModelForm):
    class Meta:
        model = PharmInfo
        exclude = ('address_id',)

请注意,这会阻止通过表单设置address_id字段(请查看文档中示例下的注释)。

答案 1 :(得分:1)

好的,好像我在模板中编辑了错误的代码。我添加了

{% for field in form.visible_fields %}

到我注释掉的模板的一部分(我发现在模板上很难看到注释行)。所有内容都按预期使用我的模板中的代码:

{% if form.non_field_errors %}
        <div class="form_errors">
            {{ form.non_field_errors }}
        </div>
    {% endif %}

    {% for field in form.visible_fields %}
    <div class="field_content">
        <label>{{ field.label_tag }}</label>  
    <div class="field">

        {% if field.help_text %}
        <div class = "help_text">
            {{ field.help_text }}
        </div>
        {% endif %}

        {{ field }}
        {{ field.errors }}
    </div> 
    </div>
    {% endfor %}

向任何花时间在这上面的人道歉(我会提前发布但是对Stackoverflow不熟悉我不能在发布后的8小时内回答我自己的问题。)