Curso Django. BBDD III. Vídeo 13

Curso Django. BBDD III. Vídeo 13

Introduction to Django Databases

In this section, we will learn about working with databases in Django. We will explore how to create models and insert, update, and delete records in the database using the Django shell.

Opening the Database and Checking Table Contents

  • To work with databases in Django, we can use the Django shell.
  • Open the shell by running python manage.py shell from within your project directory.
  • Import the model you want to work with using from <project_name>.models import <model_name>.
  • For example, if we want to work with the "articles" table, import it as from gestion_pedidos.models import Articulos.
  • Use the browser option "Show Data" to check that the table is currently empty.

Inserting Records into the Database

  • To insert a record into a table, create a variable and assign it to an instance of the model class.
  • Example: articulo = Articulos(nombre="Mesa", seccion="Decoración", precio=90)
  • Note that text fields should be enclosed in quotes while numeric fields should not.
  • Save the record by calling .save() on the variable.
  • Example: articulo.save()
  • The corresponding SQL query for this operation would be an INSERT statement.

Updating Records in the Database

  • To update a record in a table, retrieve it using a query and modify its properties.
  • Example: articulo = Articulos.objects.get(nombre="Mesa")
  • Modify properties like articulo.precio = 100.
  • Save the changes by calling .save() on the variable.
  • Example: articulo.save()
  • The corresponding SQL query for this operation would be an UPDATE statement.

Deleting Records from the Database

  • To delete a record from a table, retrieve it using a query and call the .delete() method on it.
  • Example: articulo = Articulos.objects.get(nombre="Mesa")
  • Delete the record by calling articulo.delete().
  • The corresponding SQL query for this operation would be a DELETE statement.

Conclusion

  • In this video, we learned how to work with databases in Django without writing any SQL statements.
  • We used the Django shell to insert, update, and delete records in the database using model instances.
  • These operations can also be performed using forms in Django views, which we will explore in future videos.

[t=0:07:16s] Updating and Deleting Records in Django

In this section, we will learn how to update and delete records in Django. We will explore the process of updating existing records by changing values of specific fields, as well as deleting records from the database.

Updating Records

  • To update a record, we can use the save method or the create method.
  • Using the save method:
  • Create an object and save it to a variable.
  • Use dot notation to specify the field you want to change.
  • Assign a new value to that field.
  • Save the changes by calling save() on the object.
  • Using the create method:
  • Call the create method directly on the model class.
  • Pass in the values for each field as keyword arguments.

Deleting Records

  • To delete a record, we use an SQL-like instruction called delete.
  • It is important to specify criteria when using delete to avoid accidentally deleting all records in a table.
  • To delete a specific record:
  • Create a variable and assign it to the record you want to delete using the get method with appropriate criteria.
  • Call .delete() on that variable.

Selecting Records

  • To select records from a table, we can use an SQL-like instruction called select.
  • In Django, we work with models instead of raw SQL queries.
  • To perform a select operation:
  • Create a variable and assign it to all records from a table using .all() on the model class.

This summary covers only part of the video transcript. For full content, consider watching or reading through other parts of the video.

Turn any video into a summary like this

YouTube links, meetings, lectures. With transcripts, search, and chat.

Playlists: Curso Django
Video description

En este vídeo vemos cómo insertar, actualizar y borrar registros de la BBDD. Es la base para cuando trabajemos desde formularios (que será en breve). Para más cursos, ejercicios y manuales visita: www.pildorasinformaticas.es