So I’ve connected Django to a pre-existing database successfully (inspect, validate and sync) and I’ve created an app and a project and all that (I’m reading the Django book and I’m on chapter 5), but when I actually run it and print stuff, I get an (assumed) error. While in python, I properly import what I need (from myapp.models import Artist) but if I try to print, for example, the first five rows in the table (print Artist.objects.all()[:5]), I get this:
[<Artist: Artist object>, <Artist: Artist object>, <Artist: Artist object>, <Artist: Artist object>, <Artist: Artist object>]
Why doesn’t it actually print the values instead of what seems to be a placeholder? Is there something I’m missing here?
Django uses an ORM (Object-Relational Mapper) that translates data back and forth between Python objects and database rows. So when you use it to get an item from the database, it converts it into a Python object.
If that object doesn’t define how to display itself as text, Django does it for you. Python does the same thing:
If you want to see all of the actual values for the row for each object, use
values.Here is the example from the docs: