Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Overview of Models | What is Serialization?
Django REST Framework

book
Overview of Models

Where do the data displayed on a website or mobile app come from?

For instance, if you're creating a blog, you need to store information about articles, comments, and users. Models in Django REST Framework (DRF) define the structure of this data. They serve as a way to organize and store data in your application. When you define models, DRF can use them to create an API that allows your clients (such as web pages or mobile apps) to access this data over the internet. Thus, models are the foundation of your application, enabling it to work with stored data and provide them through an API.

The Application Model We Will Develop Throughout the Course

Model will store information about a product, such as its name, description, price, available quantity, and the path to an image (here will be stored either a link to an image or the local path to it).

As we can see in the illustrations, all this information will be needed on the frontend. Therefore, on the backend, we need to store it for later send it to the frontend.

python
from django.db import models


class Product(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
price = models.DecimalField(max_digits=10, decimal_places=2)
quantity_available = models.IntegerField(default=0)
image = models.CharField(max_length=255, blank=True, null=True)

def __str__(self):
return f'Name: {self.name} price: {self.price}'

Primary Key - (pk) field

In Django, the pk field stands for "primary key." It's a special field that every record in a database table gets automatically. Its purpose is to give each record a unique identifier. For instance, if you have a table of products, each product will have its own pk. Django handles this field automatically unless you specify another field for this purpose.

question mark

What role do models play in a Django REST Framework (DRF) application?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 2

Spørg AI

expand
ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt