How to add data into ManyToMany field?

前端 未结 2 1605
不知归路
不知归路 2020-12-02 10:33

I can\'t find it anywhere, so your help will be nice for me :) Here is that field:

categories = models.ManyToManyField(fragmentCategory)

Fr

2条回答
  •  时光说笑
    2020-12-02 10:52

    There's a whole page of the Django documentation devoted to this, well indexed from the contents page.

    As that page states, you need to do:

    my_obj.categories.add(fragmentCategory.objects.get(id=1))
    

    or

    my_obj.categories.create(name='val1')
    

提交回复
热议问题