Django ManyToManyField FieldError in second app - Cannot resolve keyword 'xxxx into field
Question: I'm trying to write an application where users can submit photos. Part of this
involves voting for photos. So in my models.py I have:

`photos/models.py`

    
    
    class Photo(models.Model):
        votes = models.IntegerField(default=0)
        users_voted = models.ManyToManyField(User, related_name='voters', blank=True, null=True)
    

The users_voted ManyToManyField ensures that people can't vote twice. This all
works fine, but at the moment I'm developing another app in the project which
does MPTT comments. I want users to be able to vote each other's comments up,
too. So I copied over the voting code into my commenting model:

`comments/models.py`

    
    
    class MyMPTTComment(MPTTModel, Comment):
            parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
            cvotes = models.IntegerField(default=0)
            users_voted = models.ManyToManyField(User, related_name='cvoters')
    

This creates a table in the database as expected. But as soon as I try to
access it with my view.py code, I get the FieldError:

    
    
      def comment_vote(request):
        if request.GET.has_key('id'):
            try:
                id = request.GET['id']
                target = MyMPTTComment.objects.get(id=id)
                user_voted = target.users_voted.filter(
                username=request.user.username
                )
    

I've been scratching my head over this for a while now. Others with similar
issues found that it might be to do with the times that Django loads the
modules ( <http://chase-seibert.github.io/blog/2010/04/30/django-manytomany-
error-cannot-resolve-keyword-xxx-into-a-field.html> ) but fiddling around with
module orders to try to effect their loading times does nothing.

The error says `Choices are: ... voters`, so why can't `cvoters` be resolved?

EDIT: Adding complete error and traceback:

    
    
    Internal Server Error: /commentvote/
    Traceback (most recent call last):
      File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
      File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py", line 25, in _wrapped_view
    return view_func(request, *args, **kwargs)
      File "/root/photoproject/photos/views.py", line 300, in comment_vote
    username=request.user.username
      File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 155, in filter
    return self.get_query_set().filter(*args, **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 615, in get_query_set
    return super(ManyRelatedManager, self).get_query_set().using(db)._next_is_sticky().filter(**self.core_filters)
      File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 669, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 687, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
      File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1271, in add_q
    can_reuse=used_aliases, force_having=force_having)
      File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1139, in add_filter
    process_extras=process_extras)
      File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1337, in setup_joins
    "Choices are: %s" % (name, ", ".join(names)))
    FieldError: Cannot resolve keyword 'cvoters' into field. Choices are: comment_comments, comment_flags, date_joined, email, first_name, friend_set, groups, id, invitation, is_active, is_staff, is_superuser, last_login, last_name, logentry, moderated_messages, password, photos, received_messages, registrationprofile, sent_messages, to_friend_set, user_permissions, userbio, username, voters
    

Django v1.5.4

Posting database schema:

    
    
    CREATE TABLE "comments_mympttcomment" ("rght" integer unsigned NOT NULL, "parent_id" integer, "level" integer unsigned NOT NULL, "lft" integer unsigned NOT NULL, "tree_id" integer unsigned NOT NULL, "cvotes" integer NOT NULL, "comment_ptr_id" integer PRIMARY KEY);
    CREATE TABLE "comments_mympttcomment_users_voted" ("id" integer NOT NULL PRIMARY KEY, "mympttcomment_id" integer NOT NULL, "user_id" integer NOT NULL);
    CREATE TABLE "photos_photo" (
    "id" integer NOT NULL PRIMARY KEY,
    ...
    "votes" integer NOT NULL,
    "user_id" integer REFERENCES "auth_user" ("id"),
    );
    CREATE TABLE "photos_photo_users_voted" (
    "id" integer NOT NULL PRIMARY KEY,
    "photo_id" integer NOT NULL,
    "user_id" integer NOT NULL REFERENCES "auth_user" ("id"),
    UNIQUE ("photo_id", "user_id")
    );
    

Just noticed that the comments-user table does not include "REFERENCES
"auth_user" ("id")", but I've no idea why. As stated above, the comments model
includes the ManytoManyField exactly as the photos table does, importing from
django.contrib.auth.models import User.


Answer: I think the problem is caused by the use of multiple inheritance in
`MyMPTTComment`. Remove the `Comment` from model's definition:

    
    
    class MyMPTTComment(MPTTModel):
            parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
            cvotes = models.IntegerField(default=0)
            users_voted = models.ManyToManyField(User, related_name='cvoters')
    

You can add again the _parent_ field. I found a similar implementation to what
you want to do [here](http://codeblogging.net/blogs/1/3/).

