On May 4, 1:15 pm, JonathanB <doulo...@gmail.com> wrote:
> I'm working on a Grade Book program for my personal use. Here is the
> relevant class from my models.py file:
>
> class Grade(models.Model):
>         student = models.ForeignKey(Student)
>         assignment = models.ForeignKey(Assignment)
>         grade = models.DecimalField(max_digits=5, decimal_places=2)
>
>         def __unicode__(self):
>                 return self.grade
>
> I get the following error when I try to enter something using the
> admin interface (all the other models would save without issue): I'm
> trying to put in "10" or "10.0". I want to use a Decimal field to
> allow for 1/2 points (such as if they get the right word but the wrong
> spelling, earning a 9.5). Below is the copy/paste from the Error page
> I get when I click "Save":

<snip>

> Exception Type: TypeError at /admin/grades/grade/add/
> Exception Value: coercing to Unicode: need string or buffer, Decimal
> found
>
> So, what have I missed? Do I need to import Decimal someplace? And if
> so, where? admin.py?

The problem is in your __unicode__ method. As the name implies, it
should always actually return a unicode value, but you're just
returning the value of self.grade, which is decimal. To be safe,
Python doesn't automatically coerce anything that's not a string to
unicode - you have to do it explicitly yourself. So your method should
just be:

    def __unicode__(self):
        return unicode(self.grade)

--
DR.

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

Reply via email to