20

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?

agf
  • 171,228
  • 44
  • 289
  • 238
Gabe C.
  • 465
  • 2
  • 6
  • 12

3 Answers3

33

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.'}]
agf
  • 171,228
  • 44
  • 289
  • 238
  • Well after trying jdi's solution, data actually appeared, but this is more of what I was trying to achieve. I wish I could accept both answers! D: – Gabe C. Apr 14 '12 at 01:59
  • @GabeC.: My feelings wont be hurt. agf ended up providing the info you really needed :-) – jdi Apr 14 '12 at 02:00
  • How are we supposed to print an object's values in a simple way, when we are manipulating the model instance which doesn't have a `values()` method? – Vadorequest Aug 04 '18 at 22:25
  • values() is used for filter, but I want same this for get() method. – Harun-Ur-Rashid Nov 10 '18 at 18:00
  • Yeah, be careful folks, many ORM objects don't have a values() method and trying to call it will throw an error. – Ben Wheeler Mar 10 '21 at 02:34
7

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.

jdi
  • 90,542
  • 19
  • 167
  • 203
  • Ahh, there we go. I just defined the unicode for Artist and tried it again, and it works; thanks! :D – Gabe C. Apr 14 '12 at 01:54
0

if you want to use print method ovveride the unicode method in the model itself

 def __unicode__(self):
    return u'%s' % (self.id)

here an example model

class unit(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)
def __unicode__(self):
    return u'%s' % (self.name)

print(unit.objects.all())
[unit: KG, unit: PCs]
mohd
  • 2,634
  • 2
  • 16
  • 18
  • Could you please help me to understand how can I remove model name `unit` from `[unit: KG, unit: PCs]` for an instance, in a for loop I want to print like below: `A from KG` `B from PCs` here `A, B` will be from another model and `KG, PC` are from model `unit`. – dm90 Sep 11 '15 at 10:27
  • That is a separate question not a comment and we need to see more of your code. – Apollo Data May 15 '17 at 06:13