title: Django generic foreign key reverse filter
body: <p>I have a model called "Comments" which uses a generic foreign key:</p>
  
  <pre><code>class CommentManager(models.Manager):
  
      def for_model(self, model):
          """
          QuerySet for all comments for a particular model (either an instance or
          a class).
          """
          ct = ContentType.objects.get_for_model(model)
          qs = self.get_query_set().filter(content_type=ct)
          if isinstance(model, models.Model):
              qs = qs.filter(object_pk=force_text(model._get_pk_val()))
          return qs
  
  
  class Comment(models.Model):
      """
      A user comment about some object.
      """
      status = models.CharField(max_length=12, blank=True, null=True)
      sub_status = models.CharField(max_length=500, blank=True, null=True)
      comment = models.TextField()
  
      content_type = models.ForeignKey(
          ContentType,
          verbose_name=_('content type'),
          related_name="content_type_set_for_%(class)s")
      object_pk = models.TextField(_('object ID'))
      content_object = generic.GenericForeignKey(ct_field="content_type",
                                                 fk_field="object_pk")
  </code></pre>
  
  <p>One of the things you can put comments on are <code>Tickets</code>:</p>
  
  <pre><code>class Ticket(CommonModel):
      type = models.ForeignKey(TicketType)
      priority = models.PositiveSmallIntegerField()
      status = models.ForeignKey(TicketStatus)
      access_serial_number = models.PositiveSmallIntegerField(null=True)
      parent = models.ForeignKey('self', null=True, related_name='child')
      submitted = models.DateTimeField()
  </code></pre>
  
  <p>I do a lot of filtering of <code>Tickets</code> - in the past all I did with Comments on Tickets is that when I displayed a <code>Ticket</code>, I used <code>Comments.objects.for_model(ticket)</code> to find all the Comments for it. But now what I want to do is find <code>Tickets</code> that have a specific text in the comment. There is no <code>comment_set</code> or equivalent with <code>GenericForeignKey</code>.</p>
  
  <p>This is what I've come up with, but it's pretty horrible:</p>
  
  <pre><code>comment_ticket_ids = [int(c.object_pk) for c in Comment.objects.for_model(Ticket).filter(comment__icontains='error')]
  tickets = Ticket.filter(status=open_status, id__in=comment_ticket_ids)
  </code></pre>
  
  <p>There must be a better way.</p>