Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Say your model is 'Shop'</p> <pre><code>class Shop(models.Model): street = models.CharField(max_length=150) city = models.CharField(max_length=150) # some of your models may have explicit ordering class Meta: ordering = ('city') </code></pre> <p>Since you may have the <code>Meta</code> class <code>ordering</code> attribute set, you can use <code>order_by()</code> without parameters to clear any ordering when using <code>distinct()</code>. See the documentation under <a href="https://docs.djangoproject.com/en/dev/ref/models/querysets/#order-by" rel="noreferrer"><code>order_by</code>()</a></p> <blockquote> <p>If you don’t want any ordering to be applied to a query, not even the default ordering, call order_by() with no parameters.</p> </blockquote> <p>and <a href="https://docs.djangoproject.com/en/dev/ref/models/querysets/#distinct" rel="noreferrer"><code>distinct()</code></a> in the note where it discusses issues with using <code>distinct()</code> with ordering.</p> <p>To query your DB, you just have to call:</p> <pre><code>models.Shop.objects.order_by().values('city').distinct() # returns a dictionary </code></pre> <p>or</p> <pre><code>models.Shop.objects.order_by().values_list('city').distinct() # returns a list of tuples. </code></pre> <p>You can also add <code>flat=True</code> to <code>values_list</code> to have a flat list.</p> <p>See also: <a href="https://stackoverflow.com/questions/3432673/get-distinct-values-of-queryset-by-field">Get distinct values of Queryset by field</a></p>
    singulars
    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.
    3. 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