0
0
Djangoframework~30 mins

Defining models with fields in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
Defining Django Models with Fields
📖 Scenario: You are building a simple Django app to manage a library's book collection. Each book has a title, an author, and a number of pages.
🎯 Goal: Create a Django model called Book with fields for title, author, and pages.
📋 What You'll Learn
Create a Django model class named Book
Add a title field as a character field with max length 100
Add an author field as a character field with max length 50
Add a pages field as an integer field
💡 Why This Matters
🌍 Real World
Django models are used to define the structure of data in web applications, like books in a library system.
💼 Career
Understanding Django models is essential for backend web development jobs using Django framework.
Progress0 / 4 steps
1
Create the Book model class
Create a Django model class called Book that inherits from models.Model.
Django
Need a hint?

Use class Book(models.Model): to start your model.

2
Add the title field
Inside the Book model, add a field called title using models.CharField with max_length=100.
Django
Need a hint?

Use title = models.CharField(max_length=100) inside the class.

3
Add the author and pages fields
Add two more fields inside the Book model: author as a CharField with max_length=50, and pages as an IntegerField.
Django
Need a hint?

Use author = models.CharField(max_length=50) and pages = models.IntegerField().

4
Add string representation method
Add a __str__ method to the Book model that returns the title field.
Django
Need a hint?

Define def __str__(self): and return self.title.