Reputation: 16469
I am currently learning Django though the Django-book tutorial and I've come across an error. On chapter 5 I am supposed to input this in the python interpreter
>>> p1 = Publisher.objects.create(name='Apress',
... address='2855 Telegraph Avenue',
... city='Berkeley', state_province='CA', country='U.S.A.',
... website='http://www.apress.com/')
>>> p2 = Publisher.objects.create(name="O'Reilly",
... address='10 Fawcett St.', city='Cambridge',
... state_province='MA', country='U.S.A.',
... website='http://www.oreilly.com/')
>>> publisher_list = Publisher.objects.all()
>>> publisher_list
According to the tutorial, I should get an output of
[<Publisher: Publisher object>, <Publisher: Publisher object>]
However I get the same output but with 4 objects!!
[<Publisher: Publisher object>, <Publisher: Publisher object>, <Publisher: Publisher object>, <Publisher: Publisher object>]
Also I am supposed to change my models.py to this (added unicode functions) from django.db import models
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField()
def __unicode__(self):
return self.name
class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
def __unicode__(self):
return u'%s %s' % (self.first_name, self.last_name)
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
def __unicode__(self):
return self.title
In order to display the objects. Here is the output according to the tutorial
>>> from books.models import Publisher
>>> publisher_list = Publisher.objects.all()
>>> publisher_list
[<Publisher: Apress>, <Publisher: O'Reilly>]
But I'm still getting
[<Publisher: Publisher object>, <Publisher: Publisher object>, <Publisher: Publisher object>, <Publisher: Publisher object>]
Not sure why I'm getting more objects and why I cannot view the outputs of the unicode...
Thank you for your help!
**http://django-book.readthedocs.org/en/latest/chapter05.html is the link to the specific chapter!!!
Upvotes: 0
Views: 486
Reputation: 4391
This is because you have run the code twice. The values are already saved in the database. When you run it again, two more values get saved and they are duplicates.
Upvotes: 0
Reputation: 22808
Try this sample:
models.py
class Debt(models.Model):
user = models.ForeignKey(User)
name = models.CharField(max_length=50,
help_text="Name to identify your debt.")
due_day = models.PositiveSmallIntegerField(
help_text="Day of the month payment is due.")
def __unicode__(self):
return "{0}".format(self.user)
views.py
def debt(request):
return render(request, 'debt.html', {
'debts': Debt.objects.filter(),
})
debt.html
{% for debt in debts %}
{{debt.user}} - {{debt.name}} <br/>
{% endfor %}
Upvotes: 2