Summary
Now let's collect all the knowledge we have gained in one chapter.
Models
A Model is a class that represents a table from the database.
Model class structure
Model class should be inherited from the Model class.
class NewModel(models.Model):
The structure of the Model class should be filled in by Field objects.
# Example
attribute = models.IntegerField()
Note
Some fields have non-optional parameters (arguments).
Command for Migrations
python manage.py makemigrations
python manage.py migrate
Note
Try the
python3if thepythondoesn't work.
Instances
To work with instances, you need to use different methods.
Create
- Create a new Model class instance.
- Fill in all necessary attributes.
- Submit changes using the
save()method that send a query to the database.
post = Post()
post.title = "Title"
post.text = "Text"
post.save()
Retrieve one
Return the one database record as the Model class instance.
post = Post.objects.get(pk=pk)
Update
- Retrieve data.
- Reassign attributes of the Model class instance(s).
- Submit changes using the
save()method that send a query to the database.
post = Post.objects.get(pk=pk)
post.title = "New Title"
post.text = "New text"
post.save()
posts = Post.objects.all()
for post in posts:
post.title = "New Title"
post.text = "New text"
post.save()
URL patterns
To receive the pk from the URL address, we need to use the specific syntax: <int:pk>, where:
intis a type of value;pkis the name of the argument in the view function.
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Pergunte-me perguntas sobre este assunto
Resumir este capítulo
Mostrar exemplos do mundo real
Awesome!
Completion rate improved to 3.45
Summary
Deslize para mostrar o menu
Now let's collect all the knowledge we have gained in one chapter.
Models
A Model is a class that represents a table from the database.
Model class structure
Model class should be inherited from the Model class.
class NewModel(models.Model):
The structure of the Model class should be filled in by Field objects.
# Example
attribute = models.IntegerField()
Note
Some fields have non-optional parameters (arguments).
Command for Migrations
python manage.py makemigrations
python manage.py migrate
Note
Try the
python3if thepythondoesn't work.
Instances
To work with instances, you need to use different methods.
Create
- Create a new Model class instance.
- Fill in all necessary attributes.
- Submit changes using the
save()method that send a query to the database.
post = Post()
post.title = "Title"
post.text = "Text"
post.save()
Retrieve one
Return the one database record as the Model class instance.
post = Post.objects.get(pk=pk)
Update
- Retrieve data.
- Reassign attributes of the Model class instance(s).
- Submit changes using the
save()method that send a query to the database.
post = Post.objects.get(pk=pk)
post.title = "New Title"
post.text = "New text"
post.save()
posts = Post.objects.all()
for post in posts:
post.title = "New Title"
post.text = "New text"
post.save()
URL patterns
To receive the pk from the URL address, we need to use the specific syntax: <int:pk>, where:
intis a type of value;pkis the name of the argument in the view function.
Obrigado pelo seu feedback!