[Django]-Python (Django): 'self.text' is unsubscriptable

3πŸ‘

βœ…

The code snippet below actually works, but Pylint keeps showing me an error. Does anyone know why, please?

Yes, because Pylint does not "understand" the logic implemented in the metaclasses of a model and the fields. It thus thinks that self.text will return the TextField object, which is indeed not subscriptable. The metaclass will however "inject" a string for self.text.

There is a pylint-django package [pypi] that has a better understanding on how Django works, although it is still "limited". As one of the features it lists:

  • Fixes pylint’s knowledge of the types of Model and Form field attributes.

So normally it will fix this issue. Although it might still fail for custom model fields for example.

Leave a comment