'instancemethod' 对象没有属性 '__getitem__' [英] 'instancemethod' object has no attribute '__getitem__'
问题描述
我不断收到此错误:
TypeError: 'instancemethod' object has no attribute '__getitem__'
根据我在 django 网站上的特殊观点,我正在玩弄.我不知道为什么,但我感觉这可能与我从模型中检索特定对象的方式有关.
With my particular view on a django site I am playing around with. I have no idea why but I have a feeling it might be related to the way I am retrieving a specific object from a model.
错误出现在这里:
def myview(request):
if request.method == 'POST':
tel = request.POST.get['tel_number']
person = get_object_or_404(Employee,phone_number=tel) # HERE
我有一个带有 phone_number
作为 CharField
的 Employee 模型:
I have an Employee model with phone_number
as a CharField
as such:
class Employee(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
phone_number = models.CharField(max_length=10)
request.POST 数据是从一个 android 应用程序发送的.我想要做的就是检索具有特定 phone_number 值的 Employee 对象.
The request.POST data is sent from an android application. All I want to do is to retrieve an Employee object that has a particular phone_number value.
谢谢!
推荐答案
问题出在这一行 tel = request.POST.get['tel_number']
.如果您使用 .get
,您应该将密钥作为参数传递:
The problem is in this line tel = request.POST.get['tel_number']
. If you are using .get
you should pass the key as an argument:
tel = request.POST.get('tel_number') # if key is not present by default it will return None
否则你可以这样做:
tel = request.POST['tel_number'] # raise KeyError Exception if key is not present in dict
这篇关于'instancemethod' 对象没有属性 '__getitem__'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!