Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn 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?

Select the correct answer

question mark

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

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Ask me questions about this topic

Summarize this chapter

Show real-world examples

Awesome!

Completion rate improved to 3.57

bookDefine Other Models

Swipe to show menu

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?

Select the correct answer

question mark

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

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3
some-alt