对于指定中间模型的ManyToManyField的add() [英] add() for ManyToManyField which specifies an intermediary model

查看:775
本文介绍了对于指定中间模型的ManyToManyField的add()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关这个例外的许多问题:

There are many questions about this Exception:

AttributeError: Cannot set values on a ManyToManyField which specifies 
an intermediary model.
Use myapp.MyThroughModel's Manager instead.

docs


您不能只创建一个人与一个组之间的关系 -
,您需要指定
成员资格模型所需关系的所有细节。简单的add,create和赋值调用
不提供指定这个额外的细节的方法。因此,对于使用中间
模型的多对多关系,
被禁用。

You can’t just create a relationship between a Person and a Group - you need to specify all the detail for the relationship required by the Membership model. The simple add, create and assignment calls don’t provide a way to specify this extra detail. As a result, they are disabled for many-to-many relationships that use an intermediate model.

但是我的MyThroughModel的所有属性/列都是可选的。管理员可以创建它们。

But all my attributes/columns of MyThroughModel are optional. The manager could create them.

如何编写自定义管理器以自动创建MyThroughModel实例?

How can I write a custom manager to automatically create MyThroughModel instances?

I需要一个ORM级别的解决方案,而不是Form级别。

I need a solution at ORM-level, not Form-level.

推荐答案

我发现了一种方法来告诉django忽略额外的数据:

$ b

I found a way to tell django to ignore the extra data:

class MyThroughModel(Model):
    class Meta():
        auto_created=True
    ...

更新

这将导致django内部迁移的问题。我们更新了我们的代码,不再使用 auto_created = True

This creates issues with the django internal migrations. We updated our code and don't use auto_created = True any more.

这篇关于对于指定中间模型的ManyToManyField的add()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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