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.







On Sunday, January 7, 2018 at 10:02:22 AM UTC+5:30, utpalbr...@gmail.com 
wrote:
>
> 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
>
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/c3ed6a05-eb7a-4e8e-93f0-2d2c1faeb092%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to