'instancemethod' object has no attribute '__getitem__'

孤者浪人 提交于 2019-11-30 22:24:09

问题


I keep getting this error:

TypeError: 'instancemethod' object has no attribute '__getitem__'

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.

The error occurs here:

def myview(request):
    if request.method == 'POST':
        tel = request.POST.get['tel_number']
        person = get_object_or_404(Employee,phone_number=tel) # HERE

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)

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.

Thank you!


回答1:


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

Otherwise you can do this:

tel = request.POST['tel_number'] # raise KeyError Exception if key is not present in dict


来源:https://stackoverflow.com/questions/15666408/instancemethod-object-has-no-attribute-getitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!