I am building an API with Django that allows an external app to send posts to my API an then it inserts the received data into my database.
Here are my simplified models:
class Region(models.Model):
name = models.CharField(max_length=250)
class Event(models.Model):
name = models.CharField(max_length=250)
region = models.ManyToManyField(Region)
I receive from the api some new Event data like this:
data = {
"name": "First Event",
"region": "4" # this is the ID of the region
}
I'm trying to create a new event like this:
Event.objects.create(**data)
But I get the following error:
Direct assignment to the forward side of a many-to-many set is prohibited. Use region.set() instead.
The problem is that I can not use new_event.region.add(4)
cause region
is a string and, since this is an API, the field is always retrieved as a json key.
I read this question https://stackoverflow.com/questions/8360867/update-many-to-many-field-in-django-with-field-name-in-variable#= which is asking exactly the same, but the given answer is not working cause when I write:
new.__getattribute__(relation_name) = my_authors
as suggested there, i get the following error, which makes sense:
SyntaxError: can't assign to function call
So how can I add a manytoMany field value by the fieldName?