title: limit_choices_to in a ForeignKey that depends on an optional field in that model class
body: <p>I want to <code>limit_choices_to</code> a <code>ForeignKey(User)</code> in a model called <code>Order</code>, where the <code>User</code> is limited by <code>User</code> who must be part of <code>Place.owner</code> (M2M), where that <code>Place</code> is from <code>Order.place</code>.</p>
  
  <p>Sorry that was probably really confusing so I'll show you some models.</p>
  
  <p>First <code>Order</code>:</p>
  
  <pre><code>class Order(models.Model):
      creator     = models.ForeignKey(User, related_name='order_creator')
      place       = models.ForeignKey(Place, related_name='order_place')
      quote       = models.DecimalField(max_digits=8, decimal_places=2, null=True, blank=True)
      quote_owner = models.ForeignKey(User, related_name='order_quote_owner', limit_choices_to=Q(userprofile__user_types__name='po'), null=True, blank=True)
  </code></pre>
  
  <p>Currently as you can see I have <code>limit_choices_to</code> to simply a <code>po</code> in <code>UserProfile.user_types.name</code> (that user is a property owner). But that's just a temporary solution. The <code>quote_owner</code> is a field that specify which <code>User</code> can approve the quote.</p>
  
  <p>In other words, <em><strong>the property owner must be able to approve quotes for orders whose place the property owner owns.</strong></em></p>
  
  <p>Here is my wild (incorrect) guess:</p>
  
  <pre><code>quote_owner = models.ForeignKey(User, related_name='order_quote_owner', limit_choices_to=Q(place_set=self.place), null=True, blank=True)
  </code></pre>
  
  <p>What makes it a little complicated is that <code>Order.quote_owner</code> is optional and so is <code>Place.owners</code>. Can <code>limit_choices_to</code> be smart enough to fail silently (and not give any choices) if there are no owners specified for a place?</p>
  
  <p>Anyway, here is <code>Place</code>:</p>
  
  <pre><code>class Place(models.Model):
      name     = models.CharField(max_length=135)
      owners   = models.ManyToManyField(User, related_name='place_owners'  , limit_choices_to={'userprofile__user_types': 'po'}, null=True, blank=True)
  </code></pre>
  
  <p>Here is <code>UserProfile</code>:</p>
  
  <pre><code>class UserProfile(models.Model):
      user           = models.OneToOneField(User)
      user_types     = models.ManyToManyField(UserType, related_name='userprofile_user_types', null=True, blank=True)
      place          = models.ForeignKey(Place, related_name='userprofile_place', null=True, blank=True)
  </code></pre>
  
  <p>And finally here is <code>UserType</code>:</p>
  
  <pre><code>class UserType(models.Model):
      TYPE_CHOICES = (
          ('ad', 'administrator'   ), # 1
          ('mo', 'moderator'       ), # 2
          ('vi', 'viewer'          ), # 3
          ('pm', 'property manager'), # 4
          ('po', 'property owner'  ), # 5
          ('vm', 'vendor manager'  ), # 6
          ('ve', 'vendor'          ), # 7
          ('te', 'tenant'          ), # 8
     )
  
      name = models.CharField(max_length=2, choices=TYPE_CHOICES)
  </code></pre>
  
  <p>I kept <code>UserType</code> as an individual table because I wanted a M2M relationship between <code>User</code> and <code>UserType</code> that is still bounded by <code>choices</code>.</p>
  
  <p>I welcome and appreciate any suggestions or tips. <strong>Thanks in advance!</strong></p>