Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Testing Django Models leads to TypeError: 'str' object is not callable

I am currently testing this model:

models.py

class Tag(models.Model):
    name = models.CharField(max_length=256)
    language = models.CharField(max_length=256)

    objects = models.Manager()

    def __str__(self):
        return self.name or ''

    @property
    def tags(self):
        tags = self.tagging.values('tag')
        return tags.values('tag_id', 'tag__name', 'tag__language')

With these tests:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

test_models.py

class TagTests(TestCase):
    def setUp(self):
        self.tag_name = "name of the tag"
        self.tag_language = "language of tag"
        self.tag = Tag.objects.create(name=self.tag_name, language=self.tag_language)

    def test_name_label(self):
        tag = Tag.objects.get(name=self.tag_name)
        field_label = tag._meta.get_field('name').verbose_name()
        self.assertEqual(field_label, 'name')

    def test_tag_size(self):
        tag = Tag.objects.get(name=self.tag_name)
        max_length = tag._meta.get_field('name').max_length
        self.assertEqual(max_length, 256)

    def test_str(self):
        """Test for string representation"""
        self.assertEqual(str(self.tag), self.tag.name)

I currently get the following error for the method test_name_label(self):

FAILED frontend/tests/test_models.py::TagTests::test_name_label - TypeError: 'str' object is not callable

How can I get rid of it? What am I doing wrong here?

>Solution :

Field.verbose_name is a string, not a method. Just remove parenthesis from it:

field_label = tag._meta.get_field('name').verbose_name
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading