django-admin:创建、保存和关联 m2m 模型
我有两个模型:
class Production(models.Model):
gallery = models.ManyToManyField(Gallery)
class Gallery(models.Model):
name = models.CharField()
我的作品管理员中有 m2m 关系,但我想要这样的功能:当我创建新作品时,会创建一个默认图库,并在两者之间注册关系。
到目前为止,我可以通过覆盖生产保存来创建默认图库:
def save(self, force_insert=False, force_update=False):
if not ( Gallery.objects.filter(name__exact="foo").exists() ):
g = Gallery(name="foo")
g.save()
self.gallery.add(g)
这将创建并保存模型实例(如果它尚不存在),但我不知道如何注册两者之间的关系?
I have two models:
class Production(models.Model):
gallery = models.ManyToManyField(Gallery)
class Gallery(models.Model):
name = models.CharField()
I have the m2m relationship in my productions admin, but I want that functionality that when I create a new Production, a default gallery is created and the relationship is registered between the two.
So far I can create the default gallery by overwriting the productions save:
def save(self, force_insert=False, force_update=False):
if not ( Gallery.objects.filter(name__exact="foo").exists() ):
g = Gallery(name="foo")
g.save()
self.gallery.add(g)
This creates and saves the model instance (if it doesn't already exist), but I don't know how to register the relationship between the two?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过在
Production
上调用add
来注册关系。问题是您正在保存Gallery
,而不是您已覆盖其save
的Production
。您需要在save
结束时调用super(...).save(...)
:此外,由于您在这里处理两个模型,你应该使用 Django 的信号,可能是 post -save,这也会给你
created
标志:尽管这仍然不会达到你所说的目的;如果您确实希望将默认
Gallery
与每个新的Production
相关联,即使您没有创建默认Gallery
,您也需要这样做代码>:You register the relationship just as you have, by calling
add
on theProduction
. The problem is that you're saving theGallery
, but not theProduction
whosesave
you've overridden. You need to callsuper(...).save(...)
at the end of yoursave
:Furthermore, since you're dealing with two models here, you should use Django's signals for this, probably post-save, which will also give you the
created
flag:Though this still won't do what you say you want; if you really want to associate the default
Gallery
with every newProduction
, you'll want to do it even when you're not creating the defaultGallery
: