ModelForm displays ForeignKey table values, not choices
<p>I'm creating a Django app where each site has a single category, selected from a range of possible categories. So I have two models: <code>Site</code>, and <code>Category</code>. </p>

<p>I want a many-to-one relationship between the <code>Site</code> and <code>Category</code> models. The <code>Category</code> table contains a selection of different categories, and each <code>Site</code> record will have a foreign key referencing one of those categories. </p>

<p>models.py:</p>

<pre><code>from django.db import models
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible # for python 2 support

class Site(models.Model):
    Category = models.ForeignKey('Category')

    def __str__(self):
    return self.Name

CATEGORY_CHOICES = (
                    ('AU', 'Automobiles'),
                    ('BE', 'Beauty Products'),
                    ('GR', 'Groceries'),
                    )

class Category(models.Model):
    Category = models.CharField(choices=CATEGORY_CHOICES,max_length=2)

    def __str__(self):
    return '%s' % (self.Category)
</code></pre>

<p>forms.py:</p>

<pre><code>from django.forms import ModelForm
from sites.models import Site

class NewSiteForm(ModelForm):
    class Meta:
        model = Site
        fields = ['Category']
</code></pre>

<p>newsite.html:</p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;form method="post" action=""&gt;
{{ form}}
&lt;/form&gt;
</code></pre>

<p>Newsite.html gives me a Category dropdown that lists values already stored in my Category database, rather than listing the contents of the choices tuples defined in the Category model. so I'm getting this:</p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;form method="post" action=""&gt;
&lt;tr&gt;&lt;th&gt;&lt;label for="id_Category"&gt;Category:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;select                 id="id_Category" name="Category"&gt;
&lt;option value="" selected="selected"&gt;---------&lt;/option&gt;
&lt;option value="1"&gt;AU&lt;/option&gt;
&lt;option value="2"&gt;BA&lt;/option&gt;
&lt;option value="3"&gt;BE&lt;/option&gt;
&lt;option value="4"&gt;BO&lt;/option&gt;
&lt;option value="5"&gt;PH&lt;/option&gt;
&lt;option value="6"&gt;CO&lt;/option&gt;
&lt;option value="7"&gt;CC&lt;/option&gt;
&lt;option value="8"&gt;CB&lt;/option&gt;
&lt;option value="9"&gt;CE&lt;/option&gt;
&lt;option value="10"&gt;CS&lt;/option&gt;
&lt;option value="11"&gt;CA&lt;/option&gt;
&lt;option value="12"&gt;EA&lt;/option&gt;
&lt;option value="13"&gt;EC&lt;/option&gt;
&lt;option value="14"&gt;FA&lt;/option&gt;
&lt;option value="15"&gt;GR&lt;/option&gt;
&lt;option value="16"&gt;HA&lt;/option&gt;
&lt;option value="17"&gt;HE&lt;/option&gt;
&lt;option value="18"&gt;HG&lt;/option&gt;
&lt;option value="19"&gt;IN&lt;/option&gt;
&lt;option value="20"&gt;JE&lt;/option&gt;
&lt;option value="21"&gt;LT&lt;/option&gt;
&lt;option value="22"&gt;MS&lt;/option&gt;
&lt;option value="23"&gt;MU&lt;/option&gt;
&lt;option value="24"&gt;MI&lt;/option&gt;
&lt;option value="25"&gt;OF&lt;/option&gt;
&lt;option value="26"&gt;OU&lt;/option&gt;
&lt;option value="27"&gt;PC&lt;/option&gt;
&lt;option value="28"&gt;SH&lt;/option&gt;
&lt;option value="29"&gt;SO&lt;/option&gt;
&lt;option value="30"&gt;SP&lt;/option&gt;
&lt;option value="31"&gt;TO&lt;/option&gt;
&lt;option value="32"&gt;TY&lt;/option&gt;
&lt;option value="33"&gt;DV&lt;/option&gt;
&lt;option value="34"&gt;CL&lt;/option&gt;
&lt;option value="35"&gt;WA&lt;/option&gt;
&lt;option value="36"&gt;WI&lt;/option&gt;
&lt;/select&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/form&gt;
</code></pre>

<p>I want the form to just display the three choices defined in the model, rather than the contents of the database, like this:</p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;form method="post" action=""&gt;
&lt;tr&gt;&lt;th&gt;&lt;label for="id_Category"&gt;Category:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;select                 id="id_Category" name="Category"&gt;
&lt;option value="" selected="selected"&gt;---------&lt;/option&gt;
&lt;option value="AU"&gt;Auto&lt;/option&gt;
&lt;option value="BE"&gt;Beauty Products&lt;/option&gt;
&lt;option value="GR"&gt;Groceries&lt;/option&gt;
&lt;/select&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/form&gt;
</code></pre>

<p>What am I not understanding here and how do I make this work?</p>

<p>(Note that there will be a tuple in <code>CATEGORY_CHOICES</code> giving a readable choice for every two-letter entry stored in the <code>Category</code> table, but I am saving space by not listing all of them in this question). </p>

<p>You need to specify <code>Category</code> model:</p>

<pre><code>class NewSiteForm(ModelForm):
    class Meta:
        model = Category # &lt;--
        fields = ['Category']
</code></pre>

python django django-models django-forms modelform