0

I created an update mutation as follows, with django==3.1.4 and graphene==2.1.8 :

# models.py
class CustomUser(AbstractUser):
    # email = models.EmailField()
    firebase_id = models.CharField(max_length=50, null=True)
    nickname = models.CharField(max_length=50, null=True)
    name = models.CharField(max_length=20, null=True)
    gender = models.IntegerField(choices=Gender, default=3)
    phone = models.CharField(max_length=20, null=True)
    birthday = models.DateField(default=datetime(2020,1,1))
    address = models.CharField(max_length=200, null=True)
    profile_image = models.ImageField(default='default-avatar.png', upload_to='users/',
                                  null=True, blank=True)
class UpdateMember(graphene.Mutation):
    class Arguments:
        firebase_id = graphene.String(required=True)
        nickname = graphene.String()
        name = graphene.String()
        gender = graphene.Int()
        phone = graphene.String()
        birthday = graphene.Date()
        address = graphene.String()
        profile_image = graphene.String()

    class Meta:
        exclude = ["password"]

    member = graphene.Field(MemberType)
    success = graphene.Boolean()

    # @login_required
    @staticmethod
    def mutate(root, info, firebase_id, **kwargs):
        success = False
        member_instance = CustomUser.objects.get(firebase_id=firebase_id)

        if member_instance:
            print(member_instance)
            success = True
            for k, v in kwargs.items():
                member_instance.k = v
            member_instance.save()

            return UpdateMember(member=member_instance, success=True)
        else:
            return UpdateMember(member=None, success=False)

Running GQL below:

mutation {
  updateMember(
    firebaseId:"777",
    name:"JJJJ")
  
  {
    success
    
  }
}

Response:

{
  "data": {
    "updateMember": {
      "success": true
    }
  }
}

But I checked the database, it seems no change in it, I think .save() should have done the work persisting changes to database......

Creating Member works fine. Using PostgresQL

Could anyone figure out why?

Andy Huang
  • 347
  • 2
  • 13

1 Answers1

1

There is several issues in your code:

  1. You can not assign your model fields using string like that. See this thread
for k, v in kwargs.items():
   member_instance.k = v
   member_instance.save()

Currently your member_instance.k has nothing to do with variable k inside for loop.

  1. firebase_id field should be unique. Currently you call CustomUser.objects.get(firebase_id=firebase_id) which is risky because firebase_id is not unique field. This may lead Multiple objects error if you have more than one CustomUsers saved with same id. To fix it, just define:
class CustomUser(AbstractUser):
    # email = models.EmailField()
    firebase_id = models.CharField(max_length=50, unique=True)
    ...

To check if your member_instance has really updated. You can for example print out the values before saving it and run some test cases before final implementation. For example:

if member_instance:
            print(member_instance)
            success = True
            for k, v in kwargs.items():
                member_instance.k = v
                print(member_instance.k)
                print(k)
                print(getattr(member_instance, k))
            member_instance.save()
Viljami
  • 639
  • 7
  • 15