title: Get all possible values ​of a M2M field in a model
body: <p>I have three models : </p>
  
  <pre><code>class Country(models.Model):
      name = models.CharField(max_length=200)
  
  class City(models.Model):
      name = models.CharField(max_length=200)
      country = models.ForeignKey(Country,on_delete=models.CASCADE)
  
  class Restaurant(models.Model):
      city = models.ManyToManyField(City)
  </code></pre>
  
  <p>In my database, there is 120 cities and 25 countries. 
  But the restaurants created are located in 20 cities only.</p>
  
  <p>Problem : 
  I need a query to retrieve all the cities where my restaurants are located. And another query to retrieve all the countries where my restaurants are located.</p>
  
  <p><strong>Solution :</strong></p>
  
  <p>For the first query, i used the idea proposed by @Daniel Roseman :</p>
  
  <pre><code>City.objects.exclude(restaurant=None)
  </code></pre>
  
  <p>For the second query, I kept getting empty queryset. I had to user filter :</p>
  
  <pre><code>Country.objects.exclude(city__restaurant__isnull=False).distinct()
  </code></pre>