Django管理员强制转换为Unicode:需要字符串或缓冲区,找到元组

时间:2015-04-21 03:20:46

标签: django

我是Django的新手。我写了一些模型,然后给管理员。当我在管理员中访问这些模型(检查单个记录或添加新记录)时,它将抛出coercing to Unicode: need string or buffer, tuple found异常。

models.py

    class WorkplaceCodeNameMap(models.Model):

        workplace_code = models.CharField('单位代号', max_length = 255, primary_key = True)

        workplace_name = models.CharField('单位名称', max_length = 255, unique = True)

        def __unicode__(self):

            return self.workplace_code, self.workplace_name

    class Personnel(models.Model):

        personnel_id = models.CharField('人事编号', primary_key = True, validators = [MinLengthValidator(8)], max_length = 8)

        name = models.CharField('姓名', max_length = 255)

        workplace_code = models.CharField('单位代号', max_length = 255, null = True)

        induction_date = models.DateField('入职时间', null = True)

        aboard_date = models.DateField('入校时间', null = True)

        retirement_date = models.DateField('退休时间', null = True)

        salary_suspended_date = models.DateField('停薪时间', null = True)

        promotion_date = models.DateField('提职时间', null = True)

        rank_befor_promotion = models.CharField('提职前职级', validators = [MinLengthValidator(4)], max_length = 4, null = True)

        rank_after_promotion = models.CharField('提职后职级', validators = [MinLengthValidator(4)], max_length = 4, null = True)

        memo = models.TextField('备注', null = True)

        def __unicode__(self):

            return self.personnel_id, self.name, self.induction_date, self.aboard_date, self.retirement_date, self.salary_suspended_date, self.promotion_date, self.rank_befor_promotion, self.rank_after_promotion, self.memo

    class HouseInformation(models.Model):

        personnel= models.OneToOneField(Personnel, primary_key = True, verbose_name = '人事编号')

        couple_id = models.CharField('双职工编号', max_length = 255, null = True)

        name = models.CharField('姓名', max_length = 255)

        id_number = models.CharField('身份证号', validators = [MinLengthValidator(15)], max_length = 18, unique = True, null = True)

        current_area = models.FloatField('当前住房面积', null = True)

        original_limited_area = models.FloatField('原始限定住房面积', null = True)

        allowance_area = models.FloatField('补贴住房面积', null = True)#

        def __unicode__(self):

            return self.personnel.personnel_id, self.couple_id, self.name, self.id_number, self.current_area, self.original_limited_area, self.allowance_area

admin.py

    class PersonnelAdmin(admin.ModelAdmin):

        list_display = ('personnel_id', 'name', 'induction_date', 'aboard_date', 'retirement_date', 'salary_suspended_date', 'promotion_date', 'rank_befor_promotion', 'rank_after_promotion', 'memo')

    class HouseInformationAdmin(admin.ModelAdmin):

        list_display = ('get_personnel_id', 'couple_id', 'name', 'id_number', 'current_area', 'original_limited_area', 'allowance_area')

        def get_personnel_id(self, instance):

            return instance.personnel.personnel_id

            get_personnel_id.short_description = '人事编号'

    class WorkplaceCodeNameMapAdmin(admin.ModelAdmin):

        list_display = ('workplace_code', 'workplace_name')

我认为在__unicode__OneToOneField或admin.py中出现错误时,必定会出现错误。我还将return personnel更改为return personnel.personnel_id。它仍然无法运作。任何人都可以提出一些建议吗?

1 个答案:

答案 0 :(得分:6)

使用此行和其他类似的行,您将返回Tuple

return self.workplace_code, self.workplace_name

__unicode__()必须返回String。你可以这样做:

return "{0}, {1}".format(self.workplace_code, self.workplace_name)