我正在将graphene-django用于api。我正在尝试创建一个具有公司外键的品牌突变。当我突变时,出现以下错误“'input'是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'))
我对company = graphene.List(CompanyInput)
感到怀疑,因此我将其更改为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
}
}
}
答案 0 :(得分:0)
当您尝试将类似**args
的参数传递给print()
时,此参数将被解压缩为关键字参数,这将引发错误,因为print()
不希望有此类参数就像mutate()
方法一样。因此,您需要删除**args
:
print('args', args)
答案 1 :(得分:0)
正如其他人已经指出的那样,不清楚您正在尝试使用
实现什么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中此处更相关的讨论。