[Fixed]-How get individual fields from django model?

1đź‘Ť

If you only want to display those fields, that’s quite easy:

<form method="post" action="">
    {{ form.show_name }}<br/>
    {{ form.exhibiting_company_name }}<br/> 
    <input type="submit" value="Submit">
</form> 

But unless those are the only required fields, your form won’t validate, and you won’t know why since you’re not displaying the error messages.

IOW, you will have to define a custom form. But really, using forms.ModelForm, it’s only a couple lines of code.

Now for something totally different: having two or more models with the same schema and named “Model1”, “Model2, (…), “ModelN” is a huge design smell. If they have the same schema, they are one single model (and one single table at the db level).

0đź‘Ť

If I understood problem correctly you need to show only specific fields in template. For that you can iterate over all form’s fields (see docs) and display only those with specific names:

<form method="post" action="">
{% csrf_token %}
{% for field in form %}
    {% if field.name == "show_name" or field.name == "exhibiting_company_name" %}
        {{ field }}
    {% endif %}
{% endfor %}
<input type="submit" value="Submit">
</form> 

Leave a comment