disabled field is not passed through - workaround needed

后端 未结 7 516
夕颜
夕颜 2020-12-10 03:29

I have a form with which I want to update a MyModel object. On the model there is a unique_together constraint, fieldA together with fieldB. In the form in the clean method

7条回答
  •  暖寄归人
    2020-12-10 04:04

    I faced with similar problem and this is how I solved it.

    I set field as hidden:

    self.fields['fieldA'].widget.attrs['style'] = 'display:none;'
    

    In template I show field value separately:

    {{ form.fieldA.label_tag }}
    {{ form.fieldA }}
    {{ form.fieldA.value }}
    {{ form.fieldA.errors }}
    

    In case of fieldA is a select menu:

    {{ form.fieldA.label_tag }}
    {{ form.fieldA }}
    {% for value, title in form.fields.fieldA.choices %}
        {% if value == form.fieldA.value %}
            {{ title }}
        {% endif %}
    {% endfor %}
    {{ form.fieldA.errors }}
    

提交回复
热议问题