Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't see a need to use <code>distinct</code> here. By representing the states as separate model and using relations you would gain much more flexibility.</p> <p>I suggest you create a third <code>State</code> model and use a <a href="http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey" rel="nofollow noreferrer">ForeignKey</a> to relate the models together so that each <code>Market</code> has a <a href="http://docs.djangoproject.com/en/dev/topics/db/queries/#one-to-many-relationships" rel="nofollow noreferrer">one to many</a> relation to single <code>State</code>, i guess you also want <code>Location</code> model related to the <code>Market</code> model.</p> <pre><code>class State(models.Model): name = models.CharField('State name', max_length=150) class Market(models.Model): name = models.CharField('Market name', max_length=150) state = models.ForeignKeyField(State) class Location(models.Model): state = models.ForeignKeyField(Market) ... </code></pre> <p>In your view, all you need to do is take all the states and pass them into the template:</p> <pre><code>def locations_landing(request): state_list = State.objects.all() return render_to_response('locations.html', {'state_list':state_list}) </code></pre> <p>And finally, in your template, iterate over the state list and use a <a href="http://docs.djangoproject.com/en/dev/topics/db/queries/#backwards-related-objects" rel="nofollow noreferrer">backward relation</a> queryset to get all the markets in that state:</p> <pre><code>{% for state in state_list %} &lt;h4&gt;{{ state }}&lt;/h4&gt; {% for market in state.market_set.all %} &lt;p&gt;* &lt;a href="#"&gt;{{ market }}&lt;/a&gt;&lt;/p&gt; {% for location in market.location_set.all %} &lt;p&gt; {{ location }} &lt;/p&gt; {% endfor %} {% endfor %} {% endfor %} </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload