Reverse query name clashe with a M2M and through

956 views
Skip to first unread message

Gagaro

unread,
Oct 23, 2015, 6:01:30 AM10/23/15
to Django users
Hello,

I have a situation I don't really understand. I have the following models:

class Wishlist(models.Model):
    wine
= models.ForeignKey('Wine')
    user
= models.ForeignKey('User')


class Wine(models.Model):
    name
= models.CharField(max_length=32)


class User(models.Model):
    name
= models.CharField(max_length=32)
    wishlist
= models.ManyToManyField(Wine, through=Wishlist)

And the following error:

models_test.Wishlist.user: (fields.E303) Reverse query name for 'Wishlist.user' clashes with field name 'User.wishlist'.
HINT: Rename field 'User.wishlist', or add/change a related_name argument to the definition for field 'Wishlist.user'.

Is that an intended behaviors? In my understanding, there is no related relation created for through models (IE: we can't directly get the Wishlist instance from the User one). Why would the names clash in this case? And why does it clashes on wishlist, the reverse relation shouldn't be wishlist_set and not wishlist?

Thanks.

Simon Charette

unread,
Oct 23, 2015, 9:40:42 AM10/23/15
to Django users
Hi Gagaro,

Intermediate models are just like other in this regard, they create a related relation hence the reported clash.

You should either add related_name='+' on your Wishlist related fields or give them a unique name.

Simon

Gagaro

unread,
Oct 23, 2015, 10:27:26 AM10/23/15
to Django users
Hi Simon, thanks for the answer, it cleared some things up.

However, I still have a question. The reverse relation is wishlist_set by default. Why is Django bothered if the attribute is name wishlist (I would understand if I named my attribute wishlist_set)?

Tom Evans

unread,
Oct 23, 2015, 10:35:32 AM10/23/15
to django...@googlegroups.com
On Fri, Oct 23, 2015 at 3:27 PM, Gagaro <gaga...@gmail.com> wrote:
> However, I still have a question. The reverse relation is wishlist_set by
> default. Why is Django bothered if the attribute is name wishlist (I would
> understand if I named my attribute wishlist_set)?

The message doesn't mention the reverse relation names that conflict,
it mentions the names of *fields* whose relation names would conflict.

> Reverse query name for 'Wishlist.user' clashes with field name 'User.wishlist'.
> HINT: Rename field 'User.wishlist', or add/change a related_name argument to the definition for field 'Wishlist.user'.

'Wishlist.user' and 'User.wishlist' are the names of the fields being
discussed. The meaning might be clearer with some added words:

The reverse query name for the field 'Wishlist.user' clashes with
the field 'User.wishlist' - they are both ''wishlist_set'.
HINT: Rename field 'User.wishlist', or add/change a related_name
argument to the definition for field 'Wishlist.user'.

Cheers

Tom

Simon Charette

unread,
Oct 23, 2015, 10:40:46 AM10/23/15
to Django users
Hi Gagaro,

Upon further investigation it looks like the following thing is happening, you find have a similar example in the documentation.

When you define an explicit through model Django generates a query name to allow direct reference to the intermediate relationship.

This query name is not customizable and is generated by lower casing the intermediate model name ('wishlist' in your case and 'membership' in the linked one).

The clash you're getting here is between this implicit query name and your User.wishlist field and would manifest itself if your were to call User.objects.filter(wishlist__...) where Django couldn't differentiate the field from the through query name.

I guess the clash message could be more clear when a clash happens with this implicit query name.

Simon

Gagaro

unread,
Oct 23, 2015, 10:44:06 AM10/23/15
to Django users


On Friday, 23 October 2015 16:35:32 UTC+2, Tom Evans wrote:
'Wishlist.user' and 'User.wishlist' are the names of the fields being
discussed. The meaning might be clearer with some added words:

    The reverse query name for the field 'Wishlist.user' clashes with
the field 'User.wishlist' - they are both ''wishlist_set'.
    HINT: Rename field 'User.wishlist', or add/change a related_name
argument to the definition for field 'Wishlist.user'.

Yes but the relations names would be wishlist_set on User and user_set on Wishlist? I see no conflict there.

Gagaro

unread,
Oct 23, 2015, 10:54:09 AM10/23/15
to Django users
Indeed, that makes sense!

As you said, the issue is that the error message is exactly the same than for a "normal" conflict.

Thank you for looking it up.
Reply all
Reply to author
Forward
0 new messages