django Cannot set values on a ManyToManyField which specifies an intermediary model. Use Manager instead

前端 未结 5 904
长情又很酷
长情又很酷 2020-12-03 07:28

i am working on saving on the same form two tables - having a m2m relation. I don\'t succeed, my error persists with something like: Cannot set values on a ManyToManyField w

5条回答
  •  执笔经年
    2020-12-03 08:04

    As specified in https://docs.djangoproject.com/en/2.1/topics/db/models/

    "Unlike normal many-to-many fields, you can’t use add(), create(), or set() to create relationships"

    This means for django version 2.1 ,these methods are impossible. But the same page of django 2.2 documentation https://docs.djangoproject.com/en/2.2/topics/db/models/ tells that:

    "You can also use add(), create(), or set() to create relationships, as long as you specify through_defaults for any required fields"

    So just update the django to 2.2 or newer versions to use the above methods and while creating objects give the argument 'through_defaults' as a python dictionary with keys as your extra field names and values as their default values.

提交回复
热议问题