我希望以下列方式获取一个对象:
Collection.objects.get(name='name', type='library', owner=owner, parent=parent)
不幸的是 type
是一个关键字,因此会产生以下错误:
KeyError at /forms/create_library
type
有没有一种方法可以消除 type
一词的含义,让我可以指定该名称的字段?
最佳答案
未测试:
Collection.objects.filter(
name__exact='name',
type__exact='library',
owner__exact=owner,
parent__exact=parent)
查询文档:http://docs.djangoproject.com/en/dev/topics/db/queries/
还要考虑以不同方式命名您的字段,主要是不要与内置名称相同。
关于获取带有字段名称关键字的对象时的Django KeyError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1939815/