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

Select the correct answer

question mark

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

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 2

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Awesome!

Completion rate improved to 3.45

bookHow to create a Model?

Deslize para mostrar o menu

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?

Select the correct answer

question mark

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

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 2
some-alt