String representation with __str__ in Django models
📖 Scenario: You are building a simple Django app to manage a library's book collection. Each book has a title and an author.When you view the list of books in the Django admin or in the Django shell, you want to see a friendly name for each book instead of the default object representation.
🎯 Goal: Create a Django model called Book with fields for title and author. Then add a __str__ method to return a readable string like "Title by Author".
📋 What You'll Learn
Create a Django model named
Book with title and author fieldsAdd a
__str__ method to the Book modelThe
__str__ method should return a string formatted as "{title} by {author}"Use exact variable and method names as specified
💡 Why This Matters
🌍 Real World
Customizing the string representation of models helps developers and admins quickly identify records in Django admin and shell.
💼 Career
Knowing how to add __str__ methods is a basic skill for Django developers to improve code readability and user experience.
Progress0 / 4 steps