How to test django model method __str__()

前端 未结 2 1417
眼角桃花
眼角桃花 2021-02-14 01:35

I trying to test __str__ method, and when trying to access it in my test it returns my model instance (I think it is)

def test_str_is_equal_to_title(self):
    \         


        
相关标签:
2条回答
  • 2021-02-14 02:21

    According to the documentation:

    Model.__str__()

    The __str__() method is called whenever you call str() on an object.

    You need to call str() on the model instance:

    self.assertEqual(str(work), work.title)
    
    0 讨论(0)
  • 2021-02-14 02:33

    Alternatively, you may simply call it like:

    model_instance.__str__()

    0 讨论(0)
提交回复
热议问题