title: Set attribute of class with name of class being created
body: <p>I am trying to allow multiple models to have a connection to a <code>Review</code> model using Generic Relationships provided by <code>django.contrib.contenttypes</code>.</p>
  
  <p>Using <code>django.contrib.contenttypes.fields.GenericRelation</code> in Django 1.7 you can set a <code>related_query_name</code> on the <code>GenericRelation</code> to perform reverse queries across the relationship. <a href="https://docs.djangoproject.com/en/1.7/ref/contrib/contenttypes/#reverse-generic-relations" rel="nofollow">The django docs give this example</a>:</p>
  
  <pre><code>class Bookmark(models.Model):
      url = models.URLField()
      tags = GenericRelation(TaggedItem, related_query_name='bookmarks')
  
  &gt;&gt;&gt; # Get all tags belonging to books containing `django` in the url
  &gt;&gt;&gt; TaggedItem.objects.filter(bookmarks__url__contains='django')
  [&lt;TaggedItem: django&gt;, &lt;TaggedItem: python&gt;]
  </code></pre>
  
  <p>I want to create a decorator that has this built into models, so I dont have to define, for example, <code>related_query_name='bookmarks'</code> for the <code>Bookmark</code> model. Can this be done with a decorator? What I am thinking of is something like this:</p>
  
  <pre><code># reviews/decorators.py
  
  from django.contrib.contenttypes.fields import GenericRelation
  from .models import Review
  
  def reviewable(klass):
      # a related attr to the Review model:
      klass.reviews = GenericRelation(Review, related_query_name=klass.__name__) # need to access the name of the class being defined here
      # some other functions are added to a reviewable model here
      return klass
  </code></pre>
  
  <p>Use the decorator like this:</p>
  
  <pre><code># web_store/models.py
  
  from reviews.decorators import related_decorator
  from django.db import models
  
  @reviewable
  class Product(models.Model):
      name = models.CharField(max_length=50)
  
  @reviewable
  class Seller(models.Model):
      name = models.CharField(max_length=50)
  </code></pre>
  
  <p>Then I want to be able to query like described in the django docs above:</p>
  
  <pre><code>&gt;&gt;&gt; from reviews.models import Review
  &gt;&gt;&gt; Review.objects.filter(product__name__contains='widget')
  [&lt;Review: Blue Widget&gt;, &lt;Review: Red Widget&gt;]
  &gt;&gt;&gt; Review.objects.filter(seller__name__contains='inc')
  [&lt;Review: We Sell Stuff, Inc&gt;, &lt;Review: Cool Stuff, Inc&gt;]
  </code></pre>
  
  <p>The key here is that the reverse queries depend on the <code>related_query_name</code>. How do I access the name of the class that is being defined in a decorator? Will <code>klass.__name__</code> work?</p>
  
  <h2>Edit</h2>
  
  <p>When I try what I have described there is an issue:</p>
  
  <pre><code>&gt;&gt;&gt; Review.objects.filter(product__name__contains='television')
  Traceback (most recent call last):
    File "&lt;console&gt;", line 1, in &lt;module&gt;
    File "/Users/atheiman/programming/python/virtualenvs/simple-django/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
      return getattr(self.get_queryset(), name)(*args, **kwargs)
    File "/Users/atheiman/programming/python/virtualenvs/simple-django/lib/python2.7/site-packages/django/db/models/query.py", line 691, in filter
      return self._filter_or_exclude(False, *args, **kwargs)
    File "/Users/atheiman/programming/python/virtualenvs/simple-django/lib/python2.7/site-packages/django/db/models/query.py", line 709, in _filter_or_exclude
      clone.query.add_q(Q(*args, **kwargs))
    File "/Users/atheiman/programming/python/virtualenvs/simple-django/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1331, in add_q
      clause, require_inner = self._add_q(where_part, self.used_aliases)
    File "/Users/atheiman/programming/python/virtualenvs/simple-django/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1358, in _add_q
      current_negated=current_negated, connector=connector)
    File "/Users/atheiman/programming/python/virtualenvs/simple-django/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1182, in build_filter
      lookups, parts, reffed_aggregate = self.solve_lookup_type(arg)
    File "/Users/atheiman/programming/python/virtualenvs/simple-django/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1120, in solve_lookup_type
      _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
    File "/Users/atheiman/programming/python/virtualenvs/simple-django/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1427, in names_to_path
      self.raise_field_error(opts, name)
    File "/Users/atheiman/programming/python/virtualenvs/simple-django/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1433, in raise_field_error
      "Choices are: %s" % (name, ", ".join(available)))
  FieldError: Cannot resolve keyword 'product' into field. Choices are: anonymous, comment, comment_approved, content_type, content_type_id, created, id, object_id, score, updated, user, user_id
  </code></pre>
  
  <p>Also, the <code>GenericRelation</code> seems to break when I add it with a decorator:</p>
  
  <pre><code>&gt;&gt;&gt; product = Product.objects.all()[0]
  &gt;&gt;&gt; product.reviews
  &lt;django.contrib.contenttypes.fields.GenericRelation&gt;
  </code></pre>
  
  <p>When I add the <code>reviews</code> relationship directly in the model definition it creates a <code>GenericRelatedObjectManager</code> field, rather than a <code>GenericRelation</code> field:</p>
  
  <pre><code>&gt;&gt;&gt; product = Product.objects.all()[0]
  &gt;&gt;&gt; product.reviews
  &lt;django.contrib.contenttypes.fields.GenericRelatedObjectManager object at 0x104b06bd0&gt;
  </code></pre>