本文介绍了'instancemethod'对象没有属性'__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

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__'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 19:37