Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#22047 closed Bug (fixed)

Cannot resolve keyword u'group_ptr' into field. Choices are: group, id, name, permissions, user

Reported by: Chris Chan Owned by: Konrad Świat
Component: Database layer (models, ORM) Version: 1.6
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I recently tried to upgrade from 1.5.1 to 1.6.2 and am now getting this error for inherited many to many relationships.

Traceback (most recent call last):

File "<stdin>", line 1, in <module>
File "/opt/bg/common/lib/python2.7/site-packages/django/db/models/manager.py", line 133, in all

return self.get_queryset()

File "/opt/bg/common/lib/python2.7/site-packages/django/db/models/fields/related.py", line 549, in get_queryset

return super(ManyRelatedManager, self).get_queryset().using(db)._next_is_sticky().filter(self.core_filters)

File "/opt/bg/common/lib/python2.7/site-packages/django/db/models/query.py", line 590, in filter

return self._filter_or_exclude(False, *args, kwargs)

File "/opt/bg/common/lib/python2.7/site-packages/django/db/models/query.py", line 608, in _filter_or_exclude

clone.query.add_q(Q(*args, kwargs))

File "/opt/bg/common/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1198, in add_q

clause = self._add_q(where_part, used_aliases)

File "/opt/bg/common/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1234, in _add_q

current_negated=current_negated)

File "/opt/bg/common/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1100, in build_filter

allow_explicit_fk=True)

File "/opt/bg/common/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1357, in setup_joins

names, opts, allow_many, allow_explicit_fk)

File "/opt/bg/common/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1277, in names_to_path

"Choices are: %s" % (name, ", ".join(available)))

django.core.exceptions.FieldError: Cannot resolve keyword u'group_ptr' into field. Choices are: group, id, name, permissions, user

Change History (9)

comment:1 by Chris Chan, 11 years ago

My model looks like so.

import json, logging, hashlib, random, time, base64, sys, urlparse
from django.contrib.auth.models import User as DjangoUser, Group as DjangoGroup, GroupManager as DjangoGroupManager

class GroupManager(DjangoGroupManager):

pass

class Group(DjangoGroup):

group_name = models.CharField(max_length=80, null=False)
users = models.ManyToManyField('User', through='Membership', related_name="groups")

objects = GroupManager()

def str(self):

return self.group_name

def unicode(self):

return self.str()

def save(self, *args, kwargs):

# Per django, name needs to be unique
# But in our case, different companies can have the same group name
if not self.id:

self.name = generateSalt()

super(Group, self).save(*args, kwargs)

def generateSalt():

salt = hashlib.sha1(str(random.random())).hexdigest()[:5]
t = int(time.time())
if isinstance(t, unicode):

t = t.encode('utf-8')

return hashlib.sha1(salt + str(t)).hexdigest()

class User(DjangoUser):

deleted = models.BooleanField(default=False, null=False)
active = models.BooleanField(default=True, null=False)

def str(self):

return self.getFullName()

def unicode(self):

return unicode(self.str())

class Membership(models.Model):

user = models.ForeignKey(User)
group = models.ForeignKey(Group)
date_joined = models.DateField()

comment:2 by Chris Chan, 11 years ago

This error will happen when you try to navigate through the users' django.db.models.fields.related.ManyRelatedManager

g = Group.objects.get(id=89)
g.users.all()

comment:3 by Konrad Świat, 11 years ago

Owner: changed from nobody to Konrad Świat
Status: newassigned

comment:4 by Baptiste Mispelon, 11 years ago

I slimmed down the models a bit:

from django.db import models

from django.contrib.auth.models import User as DjangoUser, Group as DjangoGroup

class User(DjangoUser):
    pass

class Group(DjangoGroup):
    users = models.ManyToManyField(User, through='Membership', related_name='groups')

class Membership(models.Model):
    user = models.ForeignKey(User)
    group = models.ForeignKey(Group)

The problem seems to be the related_name='groups' parameter. If you remove it, things work.

I'm not fully convinced that this is a bug because having groups as a related_name actually clashes with the groups field on User.

comment:5 by AeroNotix, 11 years ago

Resolution: invalid
Status: assignedclosed

https://docs.djangoproject.com/en/dev/topics/db/models/#fields mentions that one should not create fields which clash with the API. You create a field which clashes with the API. Ergo, it doesn't work.

comment:6 by chrismedrela, 11 years ago

Has patch: set
Resolution: invalid
Status: closednew
Triage Stage: UnreviewedAccepted

@AeroNotix, you're right, but the clash should be detected by system check framework. I and mondone have written a patch: https://github.com/django/django/pull/2289.

comment:7 by Konrad Świat, 11 years ago

Triage Stage: AcceptedReady for checkin

comment:8 by Baptiste Mispelon <bmispelon@…>, 11 years ago

Resolution: fixed
Status: newclosed

In d0133504e57589dc8983a20bf488e069bddd772c:

Fixed #22047 -- detecting related_name clash with inheritance

Thanks to mondone for fruitful colaboration.

comment:9 by Chris Chan, 11 years ago

Thanks for discovering the real issue, I'll ensure there are no field and related name clashes, the error didnt help much in seeing that issue.

Note: See TracTickets for help on using tickets.
Back to Top