问题描述
我正在尝试扩展django.contrib.auth并遇到将用户添加到组的问题,这可以通过2种方法来完成。我只是想知道为什么会这样,而一个相对于另一个有什么优势呢?
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.
推荐答案
多对多关系由中间表和两个模型的外键组成。 user.groups.add(group)
将在该表中创建一个条目,其中外键指向 user
并 group
实例。 group.user_set.add(user)
也是如此。
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)
.
这篇关于user.groups.add(group)或group.user_set.add(user),哪个更好,为什么?或他们之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!