1

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
5
  • 2
    What do you mean by "isn't working"? Commented Nov 12, 2013 at 22:26
  • 1
    fix the title as well... Commented 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"? Commented 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. Commented 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! Commented Nov 12, 2013 at 22:30

1 Answer 1

6

you should use def __unicode__

answered Nov 12, 2013 at 22:26
Sign up to request clarification or add additional context in comments.

1 Comment

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.)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.