graphene-django-cud
graphene-django-cud copied to clipboard
Many to Many extras with through model
I have these models:
class Employee(models.Model):
name = models.CharField(max_length=30)
offices = models.ManyToManyField(
"Office",
through="Assignment",
blank=True,
)
class Office(models.Model):
name = models.CharField(max_length=30)
class Assignment(models.Model):
office = models.ForeignKey(
"Office",
on_delete = models.CASCADE,
)
employee = models.ForeignKey(
"Employee",
on_delete = models.CASCADE,
)
primary = models.BooleanField()
How would I go about creating a record in the Assignment model automatically with CreateEmployeeMutation.
My CreateEmployeeMutation looks like this:
class CreateEmployeeMutation(DjangoCreateMutation): # Create
class Meta:
model = Employee
many_to_many_extras = {
"offices": {
"add": {
"type": "CreateAssignmentInput",
}
}
}
The problem is that CreateAssignmentInput requires the ID of the employee which currently being created. If I change "CreateAssignmentInput" to "auto" then it asks for the fields from the Office model as if it's going to add a new office instead of a new Assignment.
Thanks for your great work!
This almost worked:
class CreateEmployeeMutation(DjangoCreateMutation):
class Meta:
model = Employee
many_to_one_extras = {
"assignment_set": {
"add": {
"type": "auto",
}
}
}
The schema gets generated and the graphiql interface says that it should work. It no longer requires an employee ID, but the mutation returns an error saying Employee has no field named 'assignment_set'
Hi!
Thanks for the report and the provided code snippets. I will have a look at this during the course of the week.
Hi. I am able to reproduce this. Scheduled for release with 0.8.0.
Hi again. After looking into this again I am unable to reproduce this.
Please see the latest test from develop: https://github.com/tOgg1/graphene-django-cud/blob/c6e630ec48a1c3204e8e7ef8c939c0f32905c789/graphene_django_cud/tests/test_create_mutation.py#L696