These are chat archives for django/django

30th
Sep 2018
MilanRgm
@MilanRgm
Sep 30 2018 04:45
How do you update in graphql?
I have this following code
class UpdateCompany(graphene.Mutation):

    class Arguments:

        input = CompanyInput(description="These fields are required", required=True)
        id = graphene.String(required=True)

    class Meta:
        description = "Update an existing company"
        # model = models.Company

    errors = graphene.String()
    company = graphene.Field(CompanyNode)

    @staticmethod
    def mutate(root, info, input=None):
        print('######################')
        print('root', root)
        print('######################')
        print('info', info, info.context, info.context.user, info.context.user.is_authenticated)
        if not info.context.user.is_authenticated:
            return UpdateCompany(errors=json.dumps('Please Login to continue'))
        try:
            company_instance = models.Company.objects.get(id=id)
            if company_instance:
                company = models.Company(owner=info.context.user,
                                         name=input.name,
                                         email=input.email,
                                         phone_number=input.phone_number,
                                         director=input.director,
                                         franchise_head=input.franchise_head
                                         )
                return UpdateCompany(company=company, instance=company_instance)
        except models.Company.DoesNotExist:
            return UpdateCompany(errors=json.dumps('No Company Exist to Update'))
my query for updating is
mutation {
  updateCompany(id: "Q29tcGFueU5vZGU6MQ==", input: {name: "Waft Company", email: "hello@gmail.com", phoneNumber: "9843698469", director: "Hello Graphql", franchiseHead: "Hello Graphql"}) {
    errors
    company {
      id
      name
    }
  }
}
I get following error message": "mutate() got an unexpected keyword argument 'id'",
MilanRgm
@MilanRgm
Sep 30 2018 07:58
I solved the issue. I was not passing id as argument.
MilanRgm
@MilanRgm
Sep 30 2018 08:29
why does user not get store in owner field?
            if company_instance:
                company_instance.name=input.name,
                company_instance.email=input.email,
                company_instance.phone_number=input.phone_number,
                company_instance.director=input.director,
                company_instance.franchise_head=input.franchise_head
                company_instance.save(owner=info.context.user)
Vaibhav Gupta
@dojutsu-user
Sep 30 2018 08:32
according to official docs, there is no argument "owner" in the "save()" method
https://docs.djangoproject.com/en/2.1/ref/models/instances/#saving-objects
MilanRgm
@MilanRgm
Sep 30 2018 08:34
i got confused with update_fields=None
i think i mixed up the confusion of the term update_fields and rest framewrk serialized_data.save(owner=request.user)
Thanks @dojutsu-user for sharing information
MilanRgm
@MilanRgm
Sep 30 2018 08:42
I strange things is happening. When i log the value of input.name i get string as waft inc but it is saved as ('waft inc',)
MilanRgm
@MilanRgm
Sep 30 2018 08:56
How can i then save it in normal string?
Vaibhav Gupta
@dojutsu-user
Sep 30 2018 08:57
It is getting saved in the normal string form only
it is shown like this
Vaibhav Gupta
@dojutsu-user
Sep 30 2018 08:59

try this

input.name[0]

and i will recommend to debug this problem.
i think the input.name is coming in this form

MilanRgm
@MilanRgm
Sep 30 2018 09:12
input.name and input.get('name') both is shown in 'waft inc' form but input.name[0] will take the first character w that i was sure but it's taking "('w')" way.
Vaibhav Gupta
@dojutsu-user
Sep 30 2018 09:14
try printing the whole "input"
print(input)
and let's see that what actually input is
MilanRgm
@MilanRgm
Sep 30 2018 09:19
this i get a normal input dict
input {'name': 'Waft Inc', 'email': 'abc@gmail.com', 'phone_number': '9812345678', 'director': 'Hello', 'franchise_head': 'Hello'}
franchise_head value is saved in normal string