You are advised to check corresponding YouTube video at the end of this article.
Let's turn Postss in Posts, and also Post Titles deserve attention.
The __str__()
method is a built-in Python method that is used to return a string representation of an object.
When defining a Django model, the __str__()
method is often used to define the human-readable representation of the object. This is particularly useful when debugging or working with the Django admin interface, where you might need to quickly identify and work with specific instances of a model.
from django.db import models
from datetime import datetime
class Posts(models.Model):
title = models.CharField(max_length = 200)
text = models.TextField()
created = models.DateTimeField(default=datetime.now)
def __str__(self):
return self.title
class Meta:
verbose_name_plural = "Posts"
No migrations needed for this simple process, which is nice.
No comments:
Post a Comment