user.groups.add(group) or group.user_set.add(user), Which is better and why ? or difference between them

大城市里の小女人 提交于 2019-12-10 18:14:07

问题


I am trying to extend django.contrib.auth and came accross adding a user into a group, which can be done in 2 ways. I was just wondering why is it like so, and what are the advantages of one over the other.


回答1:


They do the exact same thing.

A many-to-many relation consists of an intermediate table with a foreign key to both models. user.groups.add(group) will create an entry in that table where the foreign keys point to the user and group instances. The same happens with group.user_set.add(user).



来源:https://stackoverflow.com/questions/40639319/user-groups-addgroup-or-group-user-set-adduser-which-is-better-and-why-or

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!