开发者

django site administration (entering fixtures data)

I have a django sit开发者_C百科e. 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


You need to define __unicode__ method in model:

def __unicode__(self):
  return self.name
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜