Reputation: 811
So, I learning django by this http://mherman.org/blog/2012/12/30/django-basics/ tutorial and I have one problem.
I added couple books to database but in admin site I see only "App_name object". In my case I see only list of words "Books object", "Books object", "Books object" when actually I should see "War and Peace", "Brave New World", "To Kill a Mockingbird".
So, do you know what's wrong with my app?
Thank you ;)
edited: add my models.py code
from django.db import models
class Books(models.Model):
title = models.CharField(max_length=150)
author = models.CharField(max_length=100)
read = models.CharField(max_length=3)
def __unicode__(self):
return self.title + " / " + self.author + " / " + self.read
I found answer:
Django 1.5 has experimental support for Python 3, but the Django 1.5 tutorial is written for Python 2.X:
This tutorial is written for Django 1.5 and Python 2.x. If the Django version doesn’t match, you can refer to the tutorial for your version of Django or update Django to the newest version. If you are using Python 3.x, be aware that your code may need to differ from what is in the tutorial and you should continue using the tutorial only if you know what you are doing with Python 3.x.
In Python 3, you should define a str method instead of a unicode method. There is a decorator python_2_unicode_compatible which helps you to write code which works in Python 2 and 3.
@python_2_unicode_compatible class Poll(models.Model): question = models.CharField(max_length=200) pub_date = models.DateTimeField('date published')
def __str__(self): return self.question For more information see the section str and unicode methods in the Porting to Python 3 docs.
Upvotes: 3
Views: 1566
Reputation: 473833
You haven't defined (or did it incorrectly) __unicode__()
method of your Books
model:
5. Next, open up your models.py file and add these two lines of code-
def __unicode__(self): return self.title + " / " + self.author + " / " + self.read
FYI, quote from docs:
The
__unicode__()
method is called whenever you call unicode() on an object. Django usesunicode(obj)
(or the related function,str(obj)
) in a number of places. Most notably, to display an object in the Django admin site and as the value inserted into a template when it displays an object. Thus, you should always return a nice, human-readable representation of the model from the__unicode__()
method.
Upvotes: 1