title: Django: Filtering objects with results of another table?
body: <p>I'm having a mental blank on how to do this in Django, hoping you can help.</p>
  
  <p>I have a table of galleries that I am filtering by type:</p>
  
  <pre><code>public_galleries = models.Gallery.objects.filter(type = 2).filter(root_gallery__isnull = True)
  </code></pre>
  
  <p>but I also want to see if the gallery doesn't exist in the UserGallery table for a specific user. I have this list of galleries for the user:</p>
  
  <pre><code>user_galleries = models.UserGallery.objects.select_related().filter(clientuser=request.user.id).filter(gallery__root_gallery__isnull = True)
  </code></pre>
  
  <p>Note** Just started using Django for a real project, so any improvement on either of those statements are also appreciated.</p>
  
  <p>Edit - The Models:</p>
  
  <pre><code>class Gallery(models.Model):
      """Gallery model"""
      name = models.CharField(u"Gallery name", max_length=120)
      type = models.IntegerField(default=0, choices=TYPE_CHOICES)
      root_gallery = models.ForeignKey("self", blank=True, null=True)
      """ Other Fields"""
  
  class UserGallery(models.Model):
      """Model to link Gallery and ClientUser"""
      gallery = models.ForeignKey(Gallery)
      clientuser = models.ForeignKey(ClientUser)
      owner = models.BooleanField(default=False)
  </code></pre>