title: Efficient alternative at Django filter by property
body: <p>I know that <a href="https://stackoverflow.com/questions/1205375/filter-by-property">filtering by property</a> is not possible with Django, as filtering is done at database level and properties live in Python code. However, I have the following scenario:</p>
  
  <p>In one hand, I have the model <code>RegisteredUser</code> on the other hand <code>Subscription</code>. A user can have multiple subscriptions, a subscription is from one user and a user has one or none active subscriptions.</p>
  
  <p>To implement this, I have a foreign key from <code>Subscription</code> to <code>RegisteredUser</code> and a property <code>subscription</code> at <code>RegisteredUser</code> that points to the active one (latest created subscription for that user) or none if he hasn't any subscriptions.</p>
  
  <p>Which would be the most efficent way to filter users that have subscription "platinum", "gold", "silver"...? I could do a "fetch all subscriptions" and then iterate over them to check each one for a match. But it would be really expensive and if I have to do the same process for each kind of subscription type, then cost would be s * u (where <code>s</code> is the number of different subscriptions and <code>u</code> is the number of users).</p>
  
  <p>Any help will be appreciated. Thanks in advance!</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>When I first explained the problem, I didn't include all the models related to
  simplify a litte. But as you are asking me for the models and some of you haven't understood me
  (perhaps I wasn't clear enough) here you have the code.
  I've simplified the models and stripped out code that is not important now.</p>
  
  <p>What do I have here? A <code>RegisteredUser</code> can have many subscriptions (because he may change it
  as many times as he wants), and a subscription is from just one user. The user has only
  one current subscription, which is the latest one and is returned by the property
  <code>subscription</code>. <code>Subscription</code> is attached with <code>Membership</code> and this is the model whose
  <code>slug</code> can be: platinum, gold, silver, etc.</p>
  
  <p>What do I need? I need to lookup <code>Content</code> whose <code>author</code> has a specific kind of membership.
  If the property approach worked, I'd have done it like this:</p>
  
  <pre><code>Content.objects.filter(author__id__in=RegisteredUser.objects.filter(
      subscription__membership__slug="gold"))
  </code></pre>
  
  <p>But I can't do this because properties can't be used when filtering!</p>
  
  <p>I thought that I could solve the problem converting the "virtual" relation created by
  the property into a real ForeignKey, but this may cause side effects, as I should update it manually each time a user changes its subscription and now it's automatic! Any better ideas?</p>
  
  <p>Thanks so much!</p>
  
  <pre><code>class RegisteredUser(AbstractUser):
      birthdate = models.DateField(_("Birthdate"), blank=True, null=True)
      phone_number = models.CharField(_("Phone number"), max_length=9, blank=True, default="")
  
      @property
      def subscription(self):
          try:
              return self.subscriptions_set.filter(active=True).order_by("-date_joined",
                  "-created")[0]
          except IndexError:
              return None
  
  
  class Subscription(models.Model):
      date_joined = models.DateField(_("Date joined"), default=timezone.now)
      date_canceled = models.DateField(_("Date canceled"), blank=True, null=True)
      subscriber = models.ForeignKey(AUTH_USER_MODEL, verbose_name=_("Subscriber"),
          related_name="subscriptions_set")
      membership = models.ForeignKey(Membership, verbose_name=_("Membership"),
          related_name="subscriptions_set")
      created = models.DateTimeField(_("Created"), auto_now_add=True)
      last_updated = models.DateTimeField(_("Last updated"), auto_now=True)
      active = models.BooleanField(_("Active"), default=True)
  
  
  class Membership(models.Model):
      name = models.CharField(_("Name"), max_length=15)
      slug = models.SlugField(_("Slug"), max_length=15, unique=True)
      price = models.DecimalField(_("Price"), max_digits=6, decimal_places=2)
      recurring = models.BooleanField(_("Recurring"))
      duration = models.PositiveSmallIntegerField(_("Duration months"))
  
  
  class Content(models.Model):
      author = models.ForeignKey(AUTH_USER_MODEL, verbose_name=_("Author"),
          related_name="contents_set")
      title = models.CharField(_("Title"), max_length=50)
      slug = models.SlugField(_("Slug"), max_length=70, unique=True)
      content = RichTextField(_("Content"))
      date = models.DateField(_("Date"), default=timezone.now)
      published = models.BooleanField(_("Published"))
  </code></pre>