FieldError: Cannot resolve keyword u'domain' into field. Choices are: children, content, date_create, date_update, domain__associations, id, level, lft, name, parent, parent_id, rght, tree_id, user_create, user_create_id, user_update, user_update_id

570 views
Skip to first unread message

François GUÉRIN

unread,
May 17, 2016, 8:44:25 AM5/17/16
to Django users
Hi all,

I'm a django application dev for a few years, so I'm pretty familiar with it. I have a problem with a new django 1.9.6 application. I'm writing a directory app, with a main model named "Association", which have 4 m2m related fields :


class Association(models.Model):
# ... other fields ...

#: Activity domains.
#: note:: An association **MUST** have at least 1 activity, it can have some more.
domains = models.ManyToManyField(Domain, verbose_name=_('domains'), related_name='domain__associations')

#: Targeted age groups
target_groups = models.ManyToManyField(TargetGroup, verbose_name=_('target groups'),
blank=True, related_name='target_group__associations')

#: Locations where the activities of the association can take places
locations = models.ManyToManyField(Location, verbose_name=_('locations'),
blank=True, related_name='location__associations')

#: Affiliations
affiliations = models.ManyToManyField(Affiliation, verbose_name=_('affiliations'), blank=True,
related_name='affiliation__associations')


When I try to access to related data; I always have (aka, for each related field) a Field Error exception, has given in title. The keyword allways asks for the field name in the relation table, and the field list displayed are from the target table of the relation. Trlated tables are pretty simple (name, description), but domain is a mptt table.

With factory_boy, I can create new `Association` items and insert new relations to my related items, but I can't display them.

I'm stuck with this problem for a few days, that why I ask... I really don't see what I didn't do... or understand !

Thanks for your help !

Michal Petrucha

unread,
May 17, 2016, 8:51:05 AM5/17/16
to django...@googlegroups.com
Hi François,

At a quick glance, it seems the error is that you put double
underscores into related_names. That cannot work, because
related_names should follow the same restrictions as regular field
names, and field names cannot contain double underscores, since Django
uses those to separate fields when traversing relations.

Cheers,

Michal
signature.asc

François GUÉRIN

unread,
May 17, 2016, 8:56:23 AM5/17/16
to Django users
Argh !! It works ! We could use double underscores in relation names, before, no ?

François GUÉRIN

unread,
May 17, 2016, 8:58:51 AM5/17/16
to Django users
Thanks very very very much !


Le mardi 17 mai 2016 14:51:05 UTC+2, Michal Petrucha a écrit :

Michal Petrucha

unread,
May 17, 2016, 9:10:47 AM5/17/16
to django...@googlegroups.com
On Tue, May 17, 2016 at 05:58:51AM -0700, François GUÉRIN wrote:
> Thanks very very very much !

You're welcome. (-:

On Tue, May 17, 2016 at 05:56:23AM -0700, François GUÉRIN wrote:
> Argh !! It works ! We could use double underscores in relation names,
> before, no ?

I don't think so – if it ever worked, then it was only if you
restricted yourself to a very limited subset of the relationship API,
and only by accident. Double underscores are, just like field names
(or related_names) ending with underscores, impossible to support,
because of the double underscore separator in cross-relationship
lookups.

Arguably, there should be a check that would flag this as an error.
I'll try to create a test case and patch for this later today...

Cheers,

Michal
signature.asc
Reply all
Reply to author
Forward
0 new messages