Basic Shell Usage - Interactive Python Console

- Andrés Cruz

En español
Basic Shell Usage - Interactive Python Console

Este material forma parte de mi curso y libro completo; puedes adquirirlos desde el apartado de libros y/o cursos Curso y libro desarrollo web con Django 5 y Python 3 + integración con Vue 3, Bootstrap y Alpine.js.

The Django shell is a tool that we have available to test the Django project, in other words, it allows us to interact with the application using commands, it is an extremely useful tool when we want to test connections to the database using models, we can query the data structure returned from a query or similar from the command line without needing us to implement test views/controllers to do these types of checks.

This tool is available from the Django CLI using the following command:

$ python manage.py shell

Which you must run with the Django server stopped; once the previous command is executed, you should see an output similar to the following:

Python 3.X (tags/v3.12.0:0fb18b0, Oct  2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> 
From here, we can interact with the application, for example make connections to the database through the models. To do this, the first thing we must do is load the resource that you want to test:
>>> from comments.models import Comment

With this, we have loaded the model called Comment and we can perform the operations on the model.

Get all records:

>>> comments = Comment.objects.all()
<QuerySet [<Comment: Comment #1>, <Comment: Comment #2>, <Comment: Comment #3>, <Comment: Comment #4>, <Comment: Comment #5>]>
Obtain a record by PK:
>>> comment = Comment.objects.get(pk=1

In the example above, the comment with the ID of 1 must exist, if the comment does not exist, Django will throw an exception:

in get
    raise self.model.DoesNotExist(

comments.models.Comment.DoesNotExist: Comment matching query does not exist.

Or using a shortcut:

>>> from django.shortcuts get_list_or_404
>>> get_object_or_404(Comment, pk=pk)

To delete a record:

>>> comment.delete()

To save an instance of an object in the database, that is, to create or update a record in case it has the ID set:

comment.save()

To obtain data in a filtered way:

>>> Comment.objects.all().filter(text="contenido")

Where the parameter of the filter() function refers to an attribute of the model.

To get records based on a condition:

Comment.objects.get(text="contenido")

To bring ordered records:

Comment.objects.order_by('date_posted')

To delete a record:

comment.delete()

These are just some operations that we can perform with the Django ORM, but they are the ones that we are going to use in the next section to create our comments CRUD.

Andrés Cruz

Desarrollo con Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz en Udemy

Acepto recibir anuncios de interes sobre este Blog.