Django choices and dictionary
<p>I have code</p>

<pre><code>JOBS = 1
CATEGORY_CHOICES = ((JOBS, "Jobs"),)
</code></pre>

<p>And code in the model</p>

<pre><code>category = models.IntegerField(choices=CATEGORY_CHOICES, default=JOBS)
</code></pre>

<p>Instead of "jobs" I want to add a dictionary and have access to it in the template. For example</p>

<pre><code>JOBS = 1
CATEGORY_CHOICES = ((JOBS, {'title':"Jobs",'icon':"fa fa-briefcase",'slug':"jobs"}),)
</code></pre>

<p>But instead I get the following</p>

<p><a href="https://i.stack.imgur.com/Oc7Jw.png" rel="nofollow"><img src="https://i.stack.imgur.com/Oc7Jw.png" alt="enter image description here"></a></p>

<p>How to add the dictionary into the choices?
I was able to create a model, which would be set up: title and icon. But instead, I decided to create a choice. Title I can add, but for the selected item to set the icon?</p>

<p>Choices in Django models are <code>(key, value)</code> tuples. The key is what's meant to be stored in the model's field when saved and the value is meant to be what's displayed as an option. You can't simply jam a dictionary into the value.</p>

<p>For example, the below choices would store <code>human</code> in the database and display <code>Humans</code> in a select field.</p>

<pre><code>species = [
    ('human', 'Humans'),
    ('reptile', 'Reptiles'),
    ('cylons', 'Aliens'),
]
</code></pre>

<p>Instead, you need to restructure how your model your data. You should create a separate <code>Category</code> model that represents the choices which will contain fields for a slug, icon and title.</p>

<pre><code>class Category(models.Model):
    slug  = models.SlugField()
    title = models.CharField()
    icon  = models.CharField()
</code></pre>

<p>You then point your current model at the <code>Category</code> model using a <code>ForeignKey()</code>. </p>

<pre><code>class MyModel(models.Model):
    category = models.ForeignKey(Category)
</code></pre>

<p>Finally, you can use a <a href="https://docs.djangoproject.com/en/1.10/ref/forms/fields/#modelchoicefield" rel="nofollow"><code>ModelChoiceField</code></a> when rendering the form to render the related category models (related across the foreign key) as choices in a list. If you use a <a href="https://docs.djangoproject.com/en/1.10/topics/forms/modelforms/" rel="nofollow"><code>ModelForm</code></a>, all foreign keys will be represented as <a href="https://docs.djangoproject.com/en/1.10/ref/forms/fields/#modelchoicefield" rel="nofollow"><code>ModelChoiceField</code></a>s by default.</p>

python django dictionary