0

I am doing my first app on django.

I have the following class:

class SolicitanteForm(ModelForm):

    class Meta:
        model = Solicitante
        fields = ['congregacionOfertante', 'id_oferta', 'observaciones', 'descartado']

Solicitante extends the django Model class, and have other fields.

I want to use the django forms approach to render the form. But it renders only the fields defined on Meta.fields. I want it render the other fields but using <label> or <span> instead of <input> with readonly attribute.

How can I do that?

Thanks in advance.

nashuald
  • 805
  • 3
  • 14
  • 31

2 Answers2

1

For show all fields try use __all__ in Meta:

class SolicitanteForm(ModelForm):

    class Meta:
        model = Solicitante
        fields = '__all__'

For displaying the form fields is responsible widgets. So you need write own widget with necessary behavior.

In this post, author solved a similar problem, maybe it will be helpfull

Community
  • 1
  • 1
Alex Lisovoy
  • 5,767
  • 3
  • 27
  • 28
0

You can use readonly HTML attribute. It will make the input tabbable but not usable. In HTML:

<input type="text" name="congregacionOfertante" value="Cameroon" readonly>

You can custom you form as following for add the HTML attribute:

class SolicitanteForm(forms.ModelForm):
    class Meta:
        model = Solicitante
        fields = ['congregacionOfertante', 'id_oferta', 'observaciones', 'descartado']
        widgets = {
            'congregacionOfertante': forms.TextInput({'readonly': 'readonly'})
        }

Ref: HTML readonly Attribute

If you don't want it clickable/selectable use disabled attribute.

Zulu
  • 8,765
  • 9
  • 49
  • 56
  • How can I prevent [this exploit](https://github.com/blog/1068-public-key-security-vulnerability-and-mitigation) mentioned [here](https://docs.djangoproject.com/en/1.7/topics/forms/modelforms/#selecting-the-fields-to-use) using the suggest you made? – nashuald Sep 06 '14 at 18:34
  • Use the first approach to avoid the exploit. – Zulu Sep 06 '14 at 18:50