Django throws 'Direct assignment to the forward side of a many-to-many set is prohibited.' error

旧时模样 提交于 2019-12-02 00:54:25

The error should be clear; you cannot assign directly to a many-to-many field. Do it after you create the item.

conv = Contexts.objects.create(
    context_name=user_request.get("context_name"),
    context_description=user_request.get("context_description"),
    context_priority=user_request.get("context_priority"),
)
conv.users.add(user_request.get("user"))

Also note, you don't need to call save() after either create() or adding the m2m values.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!