[Fixed]-Update_or_create with ManyToManyField

16👍

It’s not possible to add tags in update_or_create because tags is a ManyToManyField and therefore involves an intermediate table for database queries.

You can however do it in two lines using get_or_create:

user = Users.objects.get_or_create(name='test_user')
user.tags.add(tag_1)

0👍

user, created = Users.objects.get_or_create(name=’test_user’)

user.tags.add(tag_1)

get_or_create return tuple

Leave a comment