Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ How to create a Model? | Models
Django: First Dive

bookHow to create a Model?

メニューを表示するにはスワイプしてください

Let's write your first model!

Django creates the models.py in your apps automatically. Find the models.py in your created app and work with it.

To write a model, you need to use the models package imported from django.db.

from django.db import models

Now, you can create tables in your database. Django is connected to SQLite3 by default.

To create a table, you need to create a model class using the following syntax:

class TableName(models.Model):

The Model class has needed tools for your models and connecting inherited classes to the database. TableName is the name of the table that you want to create.

The next step is defining the attributes of the table. The id field is created by default, you don't need to implement this attribute.

To implement other attributes (fields), you can define the class attributes and assign different fields to them.

Look at the example:

class Product(models.Model):
    name = models.CharField(max_length=100)
    price = models.FloatField()
    quantity = models.IntegerField()

Here, the CharField, FloatField, and IntegerField are classes that can receive arguments (attributes). These arguments are field parameters that can be defined by Database Management System (DBMS). In our case, DBMS is SQLite3.

Note

The existing fields you can see in the Django documentation.

1. How to create the integer attribute (field) in table?

2. How to create the string attribute (field) in table with the max length 50?

question mark

How to create the integer attribute (field) in table?

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

question mark

How to create the string attribute (field) in table with the max length 50?

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

すべて明確でしたか?

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

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

セクション 3.  2

AIに質問する

expand

AIに質問する

ChatGPT

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

セクション 3.  2
some-alt