在 Django 中向多对多关系添加记录的正确方法
首先,我计划在 Google App Engine 上运行我的项目,因此我使用 djangoappengine据我所知,它不支持 django 的 ManyToManyField
类型。因此,我像这样设置了我的模型:
from django.db import models
from django.contrib.auth.models import User
class Group(models.Model):
name = models.CharField(max_length=200)
class UserGroup(models.Model):
user = models.ForeignKey(User)
group = models.ForeignKey(Group)
在页面上,我有一个表单字段,人们可以在其中输入组名称。我希望此表单字段的结果为用户-组组合创建一个 UserGroup 对象,如果该组尚不存在,则创建一个新的 Group 对象。起初,我开始使用 add_group
方法将此逻辑放入 UserGroup 类中,但很快意识到将其放入 UserGroup
类中并没有真正意义。这样做的正确方法是什么?我看到了一些关于模型经理的东西。这是这些的用途吗?
First off, I'm planning on running my project on google app engine so I'm using djangoappengine which as far as I know doesn't support django's ManyToManyField
type. Because of this I've setup my models like this:
from django.db import models
from django.contrib.auth.models import User
class Group(models.Model):
name = models.CharField(max_length=200)
class UserGroup(models.Model):
user = models.ForeignKey(User)
group = models.ForeignKey(Group)
On a page I have a form field where people can enter a group name. I want the results from this form field to create a UserGroup object for the user - group combination and if the group doesn't yet exist create a new Group object. At first I started putting this logic in the UserGroup class with a add_group
method but quickly realized that it doesn't really make sense to put this in the UserGroup
class. What would the proper way of doing this be? I saw some stuff about model managers. Is this what those are for?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 Django 文档:
因此管理器不是创建模型新实例的方法。
我只会有一个
Group
模型的构造函数,它从关键字参数中提取User
(如果给定的话)并创建一个新的UserGroup
。因此,如果您将新的
Group
实例化为Group(name='group name', user=some_user)
构造函数可能会删除user
关键字参数并使用该用户
创建适当的UserGroup
:这样,如果您在实例化
Group
时提供用户
,则它将创建一个UserGroup
并且不会执行其他任何操作。当然,您同样可以在 UserGroup 模型的构造函数中执行类似的操作,这并不是那么重要,它只是取决于哪个隐喻对您的代码有意义。
编辑:
修复评论中指出的问题:
From the Django docs:
So a manager is not the way to create new instances of a model.
I would just have a constructor for the
Group
model which pulls theUser
(if it is given) from the keyword arguments and creates a newUserGroup
.So if you instantiated a new
Group
asGroup(name='group name', user=some_user)
the constructor could strip theuser
keyword argument away and create the appropriateUserGroup
with thatuser
:This way if you provide a
user
when instantiating yourGroup
it will create aUserGroup
and will do nothing otherwise.Of course you could equally do a similar thing in the constructor of the
UserGroup
model, it is not really that important, it just depends which metaphorically makes sense to your code.EDIT:
A fix to the problems pointed out in the comments: