[Answered ]-Related_name in many to many field

1๐Ÿ‘

โœ…

user.favorite_things and user.favs_users get same object..?

No, user.favorite_thing.all() will get you a queryset of Thing objects, whereas user.favs_user.all() will get you a queryset of related Fav objects.

The Fav objects that you get with user.favs_user.all() will thus point to the Thing objects that you get by user.favorite_thing.all(). If your Fav objects contains more data, for example the timestamp when you made that Thing your favorite, it thus can be sometimes useful to fetch the Fav objects instead and then thus enumerate over the .things that these Fav objects refer to.

The related_name=โ€ฆs look a bit odd. Typically it is the name to fetch the relation in reverse. Perhaps it is better to rename the favs_user to favorites or something.

Leave a comment