Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django admin强制使用Unicode:需要字符串或缓冲区,找到元组_Django - Fatal编程技术网

Django admin强制使用Unicode:需要字符串或缓冲区,找到元组

Django admin强制使用Unicode:需要字符串或缓冲区,找到元组,django,Django,我是Django的新手。我写了一些模型并把它们交给管理员。当我在admin中访问这些模型(检查单个记录或添加新记录)时,它将抛出强制到Unicode:need string或buffer,tuple found异常 models.py class WorkplaceCodeNameMap(models.Model): workplace_code = models.CharField('单位代号', max_length = 255, primary_key = Tru

我是Django的新手。我写了一些模型并把它们交给管理员。当我在admin中访问这些模型(检查单个记录或添加新记录)时,它将抛出
强制到Unicode:need string或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
管理员

    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')

我想一定是
\uuuuuunicode\uuuuuu
OneToOneField
中有错误,或者admin.py中有错误。我还将
返回人员
更改为
返回人员.人员id
。它仍然不起作用。有人能给点建议吗?

有了这一行和其他类似的行,您将返回一个
元组

return self.workplace_code, self.workplace_name
\uuuuUnicode\uuuu()
必须返回
字符串。您可以这样做:

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

这一行和其他类似的行返回一个
元组

return self.workplace_code, self.workplace_name
\uuuuUnicode\uuuu()
必须返回
字符串。您可以这样做:

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

需要我将
返回self.personal
更改为
返回self.personal.personal\u id
?@stamaimer如果您在House Information中调用
self.personal
,它将返回人员的
\u unicode\u()
。只要该方法返回一个字符串,调用self.personal就不会返回错误。@stamaimer我认为您应该在
\uuuunicode\uu()中返回self.personal.name
的房屋信息需要我将
返回自我.人员
更改为
返回自我.人员.人员id
?@stamaimer如果您在房屋信息中调用
自我.人员
,它将返回人员的
\uuu unicode\uuuuu()
。只要该方法返回字符串,调用self.personal就不会返回错误。@stamaimer我认为您应该在HouseInformation的
\uuuuuUnicode\uuu()
中返回self.personal.name