0

In my django models.py :

class Agent1(models.Model):
    show_name = models.CharField(db_column='Show_Name', max_length=100,null=True)
    exhibiting_company_name = models.CharField(db_column='Exhibiting_Company_Name', max_length=100,null=True) # Field name made lowercase.
    company_website = models.CharField(db_column='Company_Website', max_length=100,null=True)  # Field name made lowercase.
    company_generic_email = models.EmailField(db_column='Company_Generic_Email', max_length=100,null=True)  # Field name made lowercase.

class Agent2(models.Model):
    show_name = models.CharField(db_column='Show_Name', max_length=100,null=True)
    exhibiting_company_name = models.CharField(db_column='Exhibiting_Company_Name', max_length=100,null=True) # Field name made lowercase.
    company_website = models.CharField(db_column='Company_Website', max_length=100,null=True)  # Field name made lowercase.
    company_generic_email = models.EmailField(db_column='Company_Generic_Email', max_length=100,null=True)  # Field name made lowercase.

Like this i have around 30+ models this are just a few fields i have over 20 fields

& in my new_data.html file i have :

<form method="post"action="">{% csrf_token %}
{{ form.as_p}}
<input type="submit" name="" value="Submit">
</form>

How can i display only for e.g. show_name and exhibiting_company_name in my html template without creating a custom form in forms.py ?

Is there any way to call my model fields individually in a <input> tag like this :

<form action="demo_form.asp">
   Show Name: <input type="text" name="sname"><br/>
   Company Name: <input type="text" name="cname"><br/>
  <input type="submit" value="Submit">
</form> 

?

2 Answers2

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

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • the tables are on user basis they are different my app is named index and there are currently 1masterdb and other agent tables INDEX Agent1s Add Change Agent2s Add Change Agent3s Add Change Masterdbs Add Change <- Copied from ,my admin page – pratirup mukherjee Jan 05 '17 at 15:08
  • "the tables are on user basis" : by itself it doesn't warrant distinct models - just add a foreignkey on `user` (if "user" is a model - else just make it an int or char field with a hardcoded value identifying the "user" one way or another). And if by "different" you mean "have a common set of fields and methods plus additional per-user fields or methods" (meaning you have to write specific code for each user), then the common part should live in one base app (either as an abstract or concrete model), and the per-user customisations should live in per-user apps. – bruno desthuilliers Jan 05 '17 at 15:23
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> 
neverwalkaloner
  • 46,181
  • 7
  • 92
  • 100