【问题标题】:'input' is an invalid keyword argument for print()'input' 是 print() 的无效关键字参数
【发布时间】:2019-03-07 19:31:06
【问题描述】:

我正在使用 graphene-django 作为 api。我正在尝试创建一个突变来创建一个具有公司外键的品牌。当我变异时,我收到以下错误“'input' is an invalid keyword argument for print()”。我不知道为什么会抛出这个错误。

这是我的突变

class BrandInput(graphene.InputObjectType):

    company = graphene.List(CompanyInput)
    name = graphene.String()
    website = graphene.String()
    description = graphene.String()
    country = graphene.String()
    city = graphene.String()
    address = graphene.String()


    class CreateBrand(graphene.Mutation):

        class Arguments:

            input = BrandInput(description="These fields are required", required=True)


        class Meta:

            description = "Create Brand Mutation"

        errors = graphene.String()
        brand = graphene.Field(BrandNode)

        @staticmethod
        def mutate(root, info, **args):
            print('args', args, **args)
            if not info.context.user.is_authenticated:
                return CreateBrand(errors=json.dumps('Please Login to list your brand'))
            try:
                company = models.Company.objects.get(slug=args.get('input')['company'])
                if company:
                    brand = models.Brand.objects.create(
                        company=company,
                        name=args.get('input')['name'],
                        slug = args.get('input')['slug'],
                        website = args.get('input')['website'],
                        description = args.get('input')['description'],
                        country = args.get('input')['country'],
                        city = args.get('input')['city'],
                        address = args.get('input')['address'],
                    )
                    return CreateBrand(brand=brand, errors=null)
            except models.Company.DoesNotExist:
                return CreateBrand(errors=json.dumps('Company should be required'))

我对@9​​87654322@ 感到怀疑,所以我将其更改为company = graphene.String(),并提供了公司的信息,以便我在改变品牌时可以找到公司实例。但我得到了同样的错误。

突变的查询是

mutation {
  createBrand(input: {company: "wafty-company", name: "Wafty Brand", website: "www.wafty.com", description: "Wafty brand description", country: "Nepal", city: "Kathmandu", address: "Baneshwor", pinCode: "44600", operationDate: "2018-10-02 15:32:37", franchisingDate: "2018-10-02 15:32:37", numberOfFranchises: "0-10", numberOfOutlets: "0-10"}) {
    errors
    brand {
      name
      slug
      website
    }
  }
}

【问题讨论】:

  • 为什么要将关键字参数传递给print
  • print('args', args, **args) 调用没有意义。
  • 抱歉,这是两种编程语言的副作用。在 javascript 中我们可以,但在 python 中不行。

标签: python django graphene-python


【解决方案1】:

当您尝试将类似 **args 的参数传递给 print() 时,此参数将被解压缩为关键字参数,这将引发错误,因为 print() 不希望 mutate() 方法具有这样的参数.所以你需要删除**args

print('args', args)

【讨论】:

  • 谢谢和抱歉。
【解决方案2】:

正如其他人已经指出的那样,尚不清楚您要通过

print('args', args, **args)

线。但是,无论如何,python 的 print 函数的语法为 (ref):

print(object(s), separator=separator, end=end, file=file, flush=flush) 

并给出**<variableName> 会混淆函数,因此会吐出错误。如果你有一个kwargs 的字典并且你想打印这些值,你可以使用*kwargs.values() 的语法。例如:

args = [1, 2]
kwargs = {'a':3, 'b':4}

print(*args, *kwargs.values())

将打印

1 2 3 4

其他组合有:

print(kwargs)           # --> {'a': 3, 'b': 4}
print(*kwargs)          # --> a f
print(kwargs.values())  # --> dict_values([3, 4])
print(*kwargs.values()) # --> 3 4

this post 中的更多相关讨论。

【讨论】:

    猜你喜欢
    • 2020-07-13
    • 1970-01-01
    • 2021-12-25
    • 2017-05-04
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多