We are creating a python django model.What is the use of __str__below? What str is actually returning and where? Sample code 1
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
image = models.FileField(upload_to='profile_pics',
blank=True,default='default.jpg')
def __str__(self):
return self.user.username+ " Profile"
Sample code 2
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Article(models.Model):
title = models.CharField(max_length=100)
body = models.TextField()
date = models.DateTimeField(auto_now_add=True)
author = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.title