Django 管理强制转换为 Unicode 时出现问题
尝试基于 教程 但使用不同的模型创建 Django 应用程序。 (第一次使用 Djanago)
我正在更改管理面板以添加 3 个具有依赖外键的项目。
我知道问题源于
class EventAdmin(admin.ModelAdmin):
admin.py 的第 10 行,但我不确定应如何安排字段才能使其正常工作。
管理面板一直有效,直到我尝试创建具有 3 个选项的事件。然后我收到以下错误...强制转换为 Unicode:需要字符串或缓冲区,找到位置
代码如下...
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 django.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)
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 django.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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
.__unicode__()
方法预计返回一个unicode
对象。然而,您的 Event.__unicode__() 返回
self.location
,它是一个Location
实例。要么将self.location
转换为unicode
,要么显式引用Location
对象中的字段。The
.__unicode__()
method is expected to return aunicode
object.Your
Event.__unicode__()
however returnsself.location
which is aLocation
instance. Either have it castself.location
tounicode
or explicitly reference a field in theLocation
object.