[Answered ]-How to save many to many relationship?

2πŸ‘

βœ…

In order to create a relation you need the ids of both side. The newly created article has no id yet. If you save the article first and then add people to it it should work fine.

article = Article(
    content = content,
    author = user,
    )

article.save()

article.add(*User.objects.filter(username__in=accompanied))

The process of adding people can be cheaper by getting all users that have a username from the list of accompanied in one fetch.

πŸ‘€kanu

Leave a comment