Performing queries
Performing queries using the Django shell will give us some insight into how queries work. In the following subsections, we will discuss some common methods that are used.
Model method – all()
The all()
method returns all records found in the table for that model object. This method will return a QuerySet in the following format, representing all entries that it finds:
(virtual_env) PS > python3 manage.py shell >>> from becoming_a_django_entdev.chapter_3.models import Engine, Seller, Vehicle, VehicleModel >>> VehicleModel.objects.all() <QuerySet [<VehicleModel: Blazer LT>, <VehicleModel: Enclave Avenir>, <VehicleModel: Envision Avenir>]>
The chapter_3
data fixture only provides three VehicleModel and that is why a collection of only three objects is returned to us. Your results may vary. One of the reasons why we created a __str__()
method, as was done earlier in this chapter, in the subsection titled...