How to add multiple objects to ManyToMany relationship at once in Django ?

前端 未结 3 1770
青春惊慌失措
青春惊慌失措 2020-12-22 18:04

Based on the Django doc, I should be able to pass multiple objects at once to be added to a manytomany relationship but I get a

* TypeError:

3条回答
  •  醉话见心
    2020-12-22 19:01

    To add on, If you want to add them from a queryset

    Example

    # Returns a queryset
    permissions = Permission.objects.all()
    
    # Add the results to the many to many field (notice the *)
    
    group = MyGroup.objects.get(name='test')
    
    group.permissions.add(*permissions)
    

    From: Insert queryset results into ManytoManyfield

提交回复
热议问题