__str__ and Meta
__str__ controls how an object prints, which helps in the admin. The Meta class sets ordering, table name and constraints.
Example
class Post(models.Model):
title = models.CharField(max_length=200)
def __str__(self):
return self.title
class Meta:
ordering = ["-id"]
verbose_name_plural = "posts"Exercise
Set a text label.
def (self): return self.title
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.