Criando e Salvando Objetos ao Vivo
Insira linhas interativamente para testar ideias rapidamente
Criando e Salvando Objetos ao Vivo é uma aula grátis de Django Academy no CoddyKit. Esta é a aula 3 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Django Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Django Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
Make a Row from Scratch
The shell lets you create database rows by hand. Start by building an instance with the values you want.
>>> p = Post(title="Hello", body="My first post")Nothing Saved Yet
That instance lives only in memory so far. The database knows nothing about it until you explicitly write it.
Save to Persist
Call save() to insert the row. Django runs the SQL and your object now lives in the database for real.
>>> p.save()Django Assigns an ID
After saving, your object gets a primary key in p.pk. A value means the insert succeeded and the row exists.
>>> p.pk
1The One-Step Shortcut
Prefer fewer steps? create() builds and saves in a single call, returning the finished object right away.
>>> Post.objects.create(title="Two", body="Quick")Edit Then Save Again
To update, change an attribute and call save() once more. Django writes only the new values to that row.
>>> p.title = "Edited"
>>> p.save()Read It Back
Confirm the write by fetching the row with get(). Seeing it returned proves your data really landed.
>>> Post.objects.get(pk=1)Count Your Rows
A fast sanity check is count(). It tells you how many rows the table holds after your inserts.
>>> Post.objects.count()
2Delete When Needed
Clean up test data with delete() on an instance. The row is removed from the database immediately.
>>> p.delete()It Writes to the Real DB
Remember these changes are permanent. The shell talks to your actual database, so avoid experimenting on production data.
Great for Seeding Test Data
This live flow is perfect for seeding a few rows while you build features, so pages have something to display.
Quick Check
Which call builds and saves a row in one step?
Recap
You created objects, used save() and create() to persist them, then read, counted, and deleted rows live in the shell. 💾
Perguntas Frequentes
A aula “Criando e Salvando Objetos ao Vivo” é grátis?
Sim — o texto completo de “Criando e Salvando Objetos ao Vivo” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Django Academy, atualize para CoddyKit PRO. O curso de Django Academy inclui 4 aulas no total.
O que vou aprender em “Criando e Salvando Objetos ao Vivo”?
Insira linhas interativamente para testar ideias rapidamente Você pratica Django Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Django Academy?
Nenhuma experiência prévia é necessária. Django Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 3 de 4.
Quanto tempo leva a aula “Criando e Salvando Objetos ao Vivo”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Django Academy?
Sim. Cada aula de Django Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Iniciando python manage.py shell
- Importando e Inspecionando Modelos
- Criando e Salvando Objetos ao Vivo
- shell_plus e Ferramentas Aprimoradas