alias51
alias51

Reputation: 8638

_meta.get_field() 'method' object is not subscriptable

When trying to access the _meta api in a class based view, I get:

'method' object is not subscriptable

For example, this occurs when trying to get a verbose_name for a field using _meta.get_field in a ListView CBV:

def get_context_data(self, **kwargs):
    print(self.model._meta.get_field['email'].verbose_name)

Where am I going wrong?

Upvotes: 0

Views: 432

Answers (2)

rgermain
rgermain

Reputation: 708

get_field in _meta is function, not array

get_field(field)

def get_context_data(self, **kwargs):
    print(self.model._meta.get_field('email').verbose_name)

Upvotes: 3

crimsonpython24
crimsonpython24

Reputation: 2383

A demo from Django's documentation:

>>> User._meta.get_field('username')
<django.db.models.fields.CharField: username>

Seems like you used the wrong brackets

Upvotes: 0

Related Questions