选择中的文本错误

时间:2014-06-22 08:35:42

标签: django

我有这些模型和管理文件。它工作得很好但是当我在选择中添加新的健身房对象时选择一个活动我看到"活动对象"而不是活动标题。

我该如何更改?

# Model
from django.db import models

class Activity(models.Model):
    title = models.CharField(max_length=250)
    description = models.CharField(max_length=250)

class Gym(models.Model):
    name = models.CharField(max_length=250)
    pub_date = models.DateTimeField('date published')
    where = models.CharField(max_length=250)
    description = models.TextField()
    timetable = models.TextField()
    activities = models.ManyToManyField(Activity, through='GymActivity')

class GymActivity(models.Model):
    gym = models.ForeignKey(Gym)
    activity = models.ForeignKey(Activity)

# Admin
from django.contrib import admin
from gym.models import Gym, Activity, GymActivity

class GymActivityInline(admin.TabularInline):
    model = Gym.activities.through
    extra = 6

class GymAdmin(admin.ModelAdmin):
    list_display = ['name', 'pub_date']
    fieldsets = [
        (None,               {'fields': ['name']}),
        ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
        (None,               {'fields': ['where']}),
        (None,               {'fields': ['description']}),
        (None,               {'fields': ['timetable']}),
    ]
    inlines = [GymActivityInline]

class ActivityAdmin(admin.ModelAdmin):
    list_display = ['title']

admin.site.register(Gym, GymAdmin)
admin.site.register(Activity, ActivityAdmin)

1 个答案:

答案 0 :(得分:3)

您在模型上缺少__unicode__ method

这样做:

class Activity(models.Model):
    title = models.CharField(max_length=250)
    description = models.CharField(max_length=250)

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

If you are using Python 3.x, just replace __unicode__ with __str__