I have the following:
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=200)
def _unicode_(self):
return self.name
class Item(models.Model):
category = models.ForeignKey(Category)
dateadded = models.DateTimeField('date added')
name = models.CharField(max_length=200)
description = models.TextField()
quantity = models.IntegerField()
My problem is that def _unicode_(self) isn't working. Any ideas?
rudolph9
8,1499 gold badges55 silver badges82 bronze badges
asked Nov 12, 2013 at 22:25
Professional_Imaginer
133 bronze badges
-
2What do you mean by "isn't working"?user2555451– user25554512013年11月12日 22:26:06 +00:00Commented Nov 12, 2013 at 22:26
-
1fix the title as well...LotusUNSW– LotusUNSW2013年11月12日 22:28:02 +00:00Commented Nov 12, 2013 at 22:28
-
All special methods in Python are surrounded by double underscores on each side, not single. This is why they're called "dunder methods" instead of... "sunder methods"?abarnert– abarnert2013年11月12日 22:28:11 +00:00Commented Nov 12, 2013 at 22:28
-
I'm using this to create a sample website, and on the website I have a table called Categorys. When I go to add a new category and name it 'Books', it comes back naming it the generic 'category object' instead. I'm pretty new to python, so I'm sorry if I'm not asking this properly.Professional_Imaginer– Professional_Imaginer2013年11月12日 22:28:21 +00:00Commented Nov 12, 2013 at 22:28
-
Oh, double underscores instead of one. I didn't even realize! Can't believe it was that obvious. Thank you so much for the quick replies!Professional_Imaginer– Professional_Imaginer2013年11月12日 22:30:27 +00:00Commented Nov 12, 2013 at 22:30
1 Answer 1
you should use def __unicode__
answered Nov 12, 2013 at 22:26
vahid abdi
10.4k4 gold badges31 silver badges37 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
abarnert
Good answer, but it would be better if it explained the difference: you have two underscores before and after the name, instead of one. (With a bad-enough font, it might be hard to see if you didn't know what you were looking for.)
lang-py