Django 管理强制转换为 Unicode 时出现问题

发布于 2024-12-06 08:52:35 字数 1531 浏览 0 评论 0原文

尝试基于 教程 但使用不同的模型创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜未央樱花落 2024-12-13 08:52:35

.__unicode__() 方法预计返回一个 unicode 对象。

然而,您的 Event.__unicode__() 返回 self.location,它是一个 Location 实例。要么将 self.location 转换为 unicode,要么显式引用 Location 对象中的字段。

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

def __unicode__(self):
    return self.location.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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文