graphene-django-cud icon indicating copy to clipboard operation
graphene-django-cud copied to clipboard

Many to Many extras with through model

Open will-hanlen opened this issue 3 years ago • 4 comments

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!

will-hanlen avatar Jan 08 '21 17:01 will-hanlen

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'

will-hanlen avatar Jan 08 '21 17:01 will-hanlen

Hi!

Thanks for the report and the provided code snippets. I will have a look at this during the course of the week.

tOgg1 avatar Jan 11 '21 16:01 tOgg1

Hi. I am able to reproduce this. Scheduled for release with 0.8.0.

tOgg1 avatar Jan 13 '21 09:01 tOgg1

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

tOgg1 avatar Mar 13 '22 17:03 tOgg1