Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing Objects in Django

Tags:

python

django

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?

like image 803
Gabe C. Avatar asked Apr 14 '12 01:04

Gabe C.


People also ask

Where can I see print in Django?

If you are using apache2 server to run django application and enabled access & error logs, your print statements will be printed in the error logs. mostly apache2 logs will be in this location /var/log/apache2/ . It will print when ever it finds print command in your function.

What is q object in Django?

Q object encapsulates a SQL expression in a Python object that can be used in database-related operations. Using Q objects we can make complex queries with less and simple code. For example, this Q object filters whether the question starts wiht 'what': from django. db.

How do I get QuerySet in Django?

You get a QuerySet by using your model's Manager . Each model has at least one Manager , and it's called objects by default. Access it directly via the model class, like so: >>> Blog.objects <django.db.models.manager.Manager object at ...> >>> b = Blog(name='Foo', tagline='Bar') >>> b.objects Traceback: ...


2 Answers

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:

>>> class MyObject(object):
...     pass
... 
>>> [MyObject(), MyObject()]
[<__main__.MyObject object at 0x0480E650>,
 <__main__.MyObject object at 0x0480E350>]

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:

# This list contains a Blog object.
>>> Blog.objects.filter(name__startswith='Beatles')
[<Blog: Beatles Blog>]

# This list contains a dictionary.
>>> Blog.objects.filter(name__startswith='Beatles').values()
[{'id': 1, 'name': 'Beatles Blog', 'tagline': 'All the latest Beatles news.'}]
like image 98
agf Avatar answered Nov 05 '22 21:11

agf


UPDATE: In Python 3.x, use __str__ instead of __unicode__

What you are seeing is a list of Artist model instances. Your values are in a python object. If you would like to make the representation of those instances more helpful, you should define the __unicode__ method for them to print something useful:

https://docs.djangoproject.com/en/dev/ref/models/instances/?from=olddocs#unicode

Its not a placeholder, its the actual object's representation, converted to unicode.

like image 7
jdi Avatar answered Nov 05 '22 21:11

jdi