Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Define Other Models | Models, datatypes, and fields
/
Django ORM Ninja: Advanced Techniques for Developers

bookDefine 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?

question mark

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?

正しい答えを選んでください

question mark

What is the primary purpose of the '__str__' method in a Django model?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 2.  3

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 2.  3
some-alt