开发者

Django Admin Problem Coercing to Unicode

Trying to create a Django app based off the tutorial but using a different model.(First time using Djanago)

I'm at the part where you alter the Admin panel to add 3 items with a dependent foreign key.

I know the problem originates from the

class EventAdmin(admin.ModelAdmin):

on line 10 of admin.py but I'm not sure how the fields should be arranged to make it work.

The admin panel works untill I try to create an event with 3 choices. Then I get the following error... coercing to Unicode: need string or buffer, Location found

Code is as follows...

models.py

from django.db import models

class Location(models.Model):
    icon = models.CharField(max_length=200)
    location = models.CharField(max_length=200)

    def __unicode__(self):
        return self.location

class Event(models.Model):
    location = models.ForeignKey(Location)
    info = models.CharField(max_length=200)

    def __unicode__(self):
        return self.location

class Choice(models.Model):
    event = models.ForeignKey(Event)
    choice = models.CharField(max_length=200)
    link = models.CharField(max_length=200)

    def __unicode__(self):
        return self.choice

admin.py

from map.models import Location
from map.models import Event
from map.models import Choice
from dja开发者_如何学JAVAngo.contrib import admin

class ChoiceInline(admin.StackedInline):
    model = Choice
    extra = 4

class EventAdmin(admin.ModelAdmin):
    fieldsets = [
             (None,               {'fields': ['location', 'info']}),
             ]
    inlines = [ChoiceInline]

admin.site.register(Event, EventAdmin)
admin.site.register(Location)


The .__unicode__() method is expected to return a unicode object.

Your Event.__unicode__() however returns self.location which is a Location instance. Either have it cast self.location to unicode or explicitly reference a field in the Location object.

def __unicode__(self):
    return u'%s' % (self.location, )

def __unicode__(self):
    return self.location.location
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜