user.groups.add(group)或group.user_set.add(user),哪个更好,为什么?或他们之间的差异 [英] user.groups.add(group) or group.user_set.add(user), Which is better and why ? or difference between them

查看:236
本文介绍了user.groups.add(group)或group.user_set.add(user),哪个更好,为什么?或他们之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试扩展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),哪个更好,为什么?或他们之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆