Admin interface does not save routine object

29 views
Skip to first unread message

Andy Oram

unread,
Jan 4, 2012, 9:29:48 AM1/4/12
to Django users
I am pasting in a pretty basic models.py here. It is fairly classic (I
actually started with Poll tutorial example) and I have stripped out
anything that looks questionable. Through the standard admin
interface, I can create a User8, a Documentversion8, and a Document8.
However, after I fill out the form for Quiz8 and save it, I get a
server error (500).

---

from django.db import models

import datetime

class User8(models.Model):
name = models.CharField(max_length=150, unique=True)
creation_time = models.DateTimeField(auto_now_add=True)

def __unicode__(self):
return self.name

class Documentversion8(models.Model):
version = models.CharField(max_length=150, unique=True)
creation_time = models.DateTimeField(auto_now_add=True)

def __unicode__(self):
return self.version

class Document8(models.Model):
document_title = models.CharField(max_length=150)
document_url = models.CharField(max_length=200, unique=True)
document_owner = models.ForeignKey("User8", blank=True, null=True)
document_version = models.ForeignKey('Documentversion8', blank=True,
null=True)
creation_time = models.DateTimeField(auto_now_add=True)

def __unicode__(self):
return self.document_url

class Quiz8(models.Model):
quiz_owner = models.ForeignKey("User8", blank=True, null=True)
document = models.ForeignKey('Document8')
creation_time = models.DateTimeField(auto_now_add=True)

def __unicode__(self):
return self.document

class Question8(models.Model):
question_text = models.TextField()
# All the following must eventually be filled in, but it should be
# possible to save the question before filling them all in.
number = models.IntegerField(blank=True, null=True)
maximum = models.IntegerField(blank=True, null=True)
correct = models.IntegerField(blank=True, null=True)
quiz = models.ForeignKey('Quiz8')

def __unicode__(self):
return self.question_text

class Answer8(models.Model):
answer_text = models.TextField()
# All the following must eventually be filled in, but it should be
# possible to save the answer before filling them all in.
number = models.IntegerField(blank=True, null=True)
question = models.ForeignKey("Question8", blank=True, null=True)
# The following is filled in by the program when saving the
submitter's answer.
ipaddr = models.CharField(max_length=40, blank=True)

def __unicode__(self):
return self.answer_text

class Submission8(models.Model):
quiz = models.ForeignKey("Quiz8")
documentversion = models.ForeignKey('Documentversion8', null=True)
time = models.DateTimeField(auto_now_add=True)
answered = models.IntegerField()
unanswered = models.IntegerField()
correct = models.IntegerField()
# The following is filled in by the program when saving the
submitter's answer.
ipaddr = models.CharField(max_length=40)
creation_time = models.DateTimeField(auto_now_add=True)

def __unicode__(self):
return self.id

Brett Epps

unread,
Jan 4, 2012, 11:54:18 AM1/4/12
to django...@googlegroups.com
Hi Andy,

Could you send us a traceback from the 500 error page? (If you don't see
one, you need to set DEBUG = True in settings.py.)

Brett

>--
>You received this message because you are subscribed to the Google Groups
>"Django users" group.
>To post to this group, send email to django...@googlegroups.com.
>To unsubscribe from this group, send email to
>django-users...@googlegroups.com.
>For more options, visit this group at
>http://groups.google.com/group/django-users?hl=en.
>

Andy Oram

unread,
Jan 4, 2012, 6:07:55 PM1/4/12
to Django users
Thanks, Bill. I apologize for not including DEBUG output. Here is what
seems relevant. It looks like the ForeignKey relationship filled in
fields
with the id's of the document and document version (which is what
one would expect) and there was a problem converting it to a string.

----

Request Method: POST
Request URL: http://localhost:8000/admin/quiz9s/quiz9/add/
Django Version: 1.3.1
Exception Type: TypeError
Exception Value:

coercing to Unicode: need string or buffer, int found

Exception Location: /usr/local/lib/python2.6/dist-packages/django/
utils/encoding.py in force_unicode, line 71
Python Executable: /usr/bin/python
Python Version: 2.6.5

-------

csrfmiddlewaretoken: u'267144fccb830df5fa96feeb691b7ae8'

document: u'1'

quiz_owner: u'1'

_save: u'Save'

Brett Epps

unread,
Jan 4, 2012, 6:26:48 PM1/4/12
to django...@googlegroups.com
Oh, I think the problem is in the __unicode__() method of your Quiz8
model. You're returning self.document, which is a Document8 object and
not a unicode string. Try returning unicode(self.document) or something
like '%d' % document.id.

Brett

Andy Oram

unread,
Jan 5, 2012, 10:12:32 AM1/5/12
to Django users
Yes (and sorry for getting your name wrong before, Brett).
str(self.document) works.
Reply all
Reply to author
Forward
0 new messages