问题
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