Define Other Models
Let's continue to create our application. So, we continue to write our Models inside the models.py file.
from django.db import models
class Author(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
pen_name = models.CharField(max_length=100)
def __str__(self):
if self.pen_name:
return f"Author {self.pk}: {self.pen_name}"
else:
return f"Author {self.pk}: {self.first_name} {self.last_name}"
class Genre(models.Model):
format = models.CharField(max_length=50, unique=True)
def __str__(self):
return f"Literary genre {self.pk}: {self.format}"
class Book(models.Model):
COVER_CHOICES = [
("H", "Hard"),
("S", "Soft")
]
title = models.CharField(max_length=100, unique=True)
price = models.DecimalField(max_digits=5, decimal_places=2)
cover = models.CharField(max_length=1, choices=COVER_CHOICES)
def __str__(self):
return f"Book {self.pk}: {self.title}"
Note
An id field is automatically added as a primary key for each model instance. You don't need to declare it explicitly in your models.
We haven't created any relations between our Models yet, but we'll address this shortly.
1. In the Book model, you wish to add three new fields: 'publishing_date', 'rating', and 'quantity'. Which combination of field types is the most appropriate for these fields?
2. What is the primary purpose of the '__str__' method in a Django model?
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Ställ mig frågor om detta ämne
Sammanfatta detta kapitel
Visa verkliga exempel
Awesome!
Completion rate improved to 3.57
Define Other Models
Svep för att visa menyn
Let's continue to create our application. So, we continue to write our Models inside the models.py file.
from django.db import models
class Author(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
pen_name = models.CharField(max_length=100)
def __str__(self):
if self.pen_name:
return f"Author {self.pk}: {self.pen_name}"
else:
return f"Author {self.pk}: {self.first_name} {self.last_name}"
class Genre(models.Model):
format = models.CharField(max_length=50, unique=True)
def __str__(self):
return f"Literary genre {self.pk}: {self.format}"
class Book(models.Model):
COVER_CHOICES = [
("H", "Hard"),
("S", "Soft")
]
title = models.CharField(max_length=100, unique=True)
price = models.DecimalField(max_digits=5, decimal_places=2)
cover = models.CharField(max_length=1, choices=COVER_CHOICES)
def __str__(self):
return f"Book {self.pk}: {self.title}"
Note
An id field is automatically added as a primary key for each model instance. You don't need to declare it explicitly in your models.
We haven't created any relations between our Models yet, but we'll address this shortly.
1. In the Book model, you wish to add three new fields: 'publishing_date', 'rating', and 'quantity'. Which combination of field types is the most appropriate for these fields?
2. What is the primary purpose of the '__str__' method in a Django model?
Tack för dina kommentarer!