tags:

views:

17

answers:

1

I have a django site. a snippet of myapp/models.py looks like this:

class Continent(models.Model):
    name = models.CharField(max_length=128, db_index=True, unique=True)


class GeographicRegion(models.Model):
    continent = models.ForeignKey(Continent, null=False)
    name =  models.CharField(max_length=128, null=False)

When I am attempting to add new geographic regions, when I click the drop down list (select control) to choose a continent, the only values (options) in the drop down list are:

continent object
continent object
continent object
continent object
continent object
continent object

I was expecting to see the names of the continents instead, so I could create relevant geographic regions per continent. How do I get the name to show in the list instead of 'continent object'?

Hint: i added str(self) and repr(self) methods to my ContinentAdmin model in myapp/admin.py like this:

myapp/admin.py

 58 class ContinentAdmin(admin.ModelAdmin):
 59    def __repr__(self):
 60        return self.name
 61 
 62 admin.site.register(Continent,ContinentAdmin)

, however this had no (observable) effect

+2  A: 

You need to define __unicode__ method in model:

def __unicode__(self):
  return self.name
Anpher
Yup, that seems to have done the trick. Where is this documented? - so I can book mark it. Also, is there anyway I can display the id of the object 9as stored in the database), without having to look directly at the database?
skyeagle
@skyeagle it all is documented here: http://docs.djangoproject.com/en/dev/ref/contrib/admin/ I believe youre looking for list_display option for AdminModel
Anpher
Thanks for that!
skyeagle