What is doing __str__ function in Django?

前端 未结 6 2282
猫巷女王i
猫巷女王i 2021-02-13 13:09

I\'m reading and trying to understand django documentation so I have a logical question.

There is my models.py file

from django.db import mo         


        
6条回答
  •  故里飘歌
    2021-02-13 13:55

    The __str__ function is used add a string representation of a model's object, so that is

    to tell Python what to do when we convert a model instance into a string.

    And if you dont mention it then it will take it by default the USERNAME_FIELD for that purpose.

    So in above example it will convert Blog and Author model's object to their associated name field and the objects of Post model to their associated headline field

提交回复
热议问题