Error mutation grapiql django many-to-many set is prohibited. Use films.set() instead
Tengo los siguientes modelos:
######################################################
class People(TimeStampedModel, SimpleNameModel):
""" Personajes del universo de Star Wars """
MALE = 'male'
FEMALE = 'female'
HERMAPHRODITE = 'hermaphrodite'
NA = 'n/a'
GENDER = (
(MALE, 'Male'),
(FEMALE, 'Female'),
(HERMAPHRODITE, 'Hermaphrodite'),
(NA, 'N/A'),
)
HAIR_COLOR_CHOICES = (
('BLACK', 'black'),
('BROWN', 'brown'),
('BLONDE', 'blonde'),
('RED', 'red'),
('WHITE', 'white'),
('BALD', 'bald'),
)
EYE_COLOR_CHOICES = (
('BLACK', 'black'),
('BROWN', 'brown'),
('YELLOW', 'yellow'),
('RED', 'red'),
('GREEN', 'green'),
('PURPLE', 'purple'),
('UNKNOWN', 'unknown'),
)
height = models.CharField(max_length=16, blank=True)
mass = models.CharField(max_length=16, blank=True)
# hair_color = models.CharField(max_length=32, blank=True)
hair_color = models.CharField(max_length=32, choices=HAIR_COLOR_CHOICES, blank=True)
skin_color = models.CharField(max_length=32, blank=True)
# eye_color = models.CharField(max_length=32, blank=True)
eye_color = models.CharField(max_length=32, choices=EYE_COLOR_CHOICES, blank=True)
birth_year = models.CharField(max_length=16, blank=True)
gender = models.CharField(max_length=64, choices=GENDER)
home_world = models.ForeignKey(Planet, on_delete=models.CASCADE, related_name='inhabitants')
class Meta:
db_table = 'people'
verbose_name_plural = 'people'
######################################################
class Film(TimeStampedModel):
title = models.CharField(max_length=100)
episode_id = models.PositiveSmallIntegerField() # TODO: Agregar choices
opening_crawl = models.TextField(max_length=1000)
release_date = models.DateField()
director = models.ForeignKey(Director, on_delete=models.CASCADE, related_name='films')
producer = models.ManyToManyField(Producer, related_name='films')
characters = models.ManyToManyField(People, related_name='films', blank=True)
planets = models.ManyToManyField(Planet, related_name='films', blank=True)
class Meta:
db_table = 'film'
def __str__(self):
return self.title
######################
class People_film(People):
films = models.ManyToManyField(Film, related_name='film', blank=True)
Estoy haciendo una mutacion en graphiql de creación que al agregar people tambien pueda agregar films en las que estuvieron
mutation {
createPeople(name: "jhonatan ibarra", height: "ejemplo", birthYear: "00/00/000",
eyeColor: "red", gender: "male", hairColor: "red", mass: "ejemplo", skinColor:
"red", homeWorldId: 2, peopleFilmId:"Revenge of the Sith") {
people {
name
}
}
}
Pero sale el siguiente error :
{
"errors": [
{
"message": "Direct assignment to the reverse side of a many-to-many set is
prohibited. Use films.set() instead.",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"createPeople"
]
}
],
"data": {
"createPeople": null
}
}
EL codigo de creacón de(mutación es el siguiente):
class CreatePeople(graphene.Mutation):
people = graphene.Field(PeopleFilmType)
class Arguments:
name= graphene.String()
height = graphene.String()
mass = graphene.String()
hair_color = graphene.String()
skin_color = graphene.String()
eye_color = graphene.String()
birth_year = graphene.String()
gender = graphene.String()
home_world_id = graphene.Int()
people_film_id = graphene.String()
def mutate(self, info, name, height,mass,hair_color,skin_color,eye_color,birth_year,gender,home_world_id, people_film_id):
p = People(name=name, height=height,mass=mass,hair_color=hair_color,skin_color=skin_color,eye_color=eye_color,birth_year=birth_year,gender=gender)
hw = Planet.objects.get(id=home_world_id)
pf = Film.objects.get(title=people_film_id)
p.home_world = hw
p.films = pf
p.save()
return CreatePeople(people=p)
Como podría solucionar este error, en la mutación. muchas gracias