Can you people explain why the def _str_() method is used below?

46 views
Skip to first unread message

utpalbr...@gmail.com

unread,
Jan 6, 2018, 11:32:22 PM1/6/18
to Django users
from django.db import models

class Blog(models.Model):
    name = models.CharField(max_length=100)
    tagline = models.TextField()

    def __str__(self):              # __unicode__ on Python 2
        return self.name

class Author(models.Model):
    name = models.CharField(max_length=200)
    email = models.EmailField()

    def __str__(self):              # __unicode__ on Python 2
        return self.name

class Entry(models.Model):
    blog = models.ForeignKey(Blog, on_delete=models.CASCADE)
    headline = models.CharField(max_length=255)
    body_text = models.TextField()
    pub_date = models.DateField()
    mod_date = models.DateField()
    authors = models.ManyToManyField(Author)
    n_comments = models.IntegerField()
    n_pingbacks = models.IntegerField()
    rating = models.IntegerField()

    def __str__(self):              # __unicode__ on Python 2
        return self.headline

James Schneider

unread,
Jan 7, 2018, 12:36:22 AM1/7/18
to django...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4f8a3ff4-b0aa-4357-bff2-3ca1dc907de3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

projec...@gmail.com

unread,
Jan 7, 2018, 9:34:53 AM1/7/18
to Django users
That is returning a string expression for your model. I will explain with an example.
Consider you were to pull the list of authors in one of your template in a choice field. If you do not have a string method your choice field options by default will look like:
  • author.objects(1)
  • author.objects(2)
and so on. Basically, you will get a list of objects in return. However, that is not how you would want your users to see. Your users would want to know the text/value/name of the author.  That's when you put a string method to your model. Now if you pull the list of objects you will get it as:
  • James
  • jack
So this method returns a string representation of your model. Hope this helps.

utpalbr...@gmail.com

unread,
Jan 9, 2018, 2:27:20 PM1/9/18
to Django users
Thanks alot James and Projec.Your posts were very helpful to me.

Furbee

unread,
Jan 9, 2018, 2:30:52 PM1/9/18
to django...@googlegroups.com
Who you call "you people?" Just kidding.

On Tue, Jan 9, 2018 at 11:27 AM, <utpalbr...@gmail.com> wrote:
Thanks alot James and Projec.Your posts were very helpful to me.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages