在Django admin

时间:2017-07-29 14:12:59

标签: python django python-2.7

我在Django上很新,并且在admin中使用带有西里尔符号的search_fields时会出现UnicodeEncodeError。使用ascii符号搜索工作正常。我应该寻找什么?

Exception Type: UnicodeEncodeError at /admin/persons/person/
Exception Value: 'ascii' codec can't encode characters in position 0-11: ordinal not in range(128)

models.py

class Person(GenericModel, DatePeriodMixin):
    last_name = models.CharField(_(u'фамилия'), max_length=255)
    first_name = models.CharField(_(u'имя'), max_length=255, blank=True)
    patronymic = models.CharField(_(u'отчество'), max_length=255, blank=True)

    class Meta:
        verbose_name = _('персоналия')
        verbose_name_plural = _('персоналии')
        ordering = ['last_name', 'first_name', 'patronymic']

    def __unicode__(self):
        return u'{} {} {}'.format(
            self.last_name,
            self.first_name,
            self.patronymic).rstrip()

    def get_absolute_url(self):
        return reverse('persons:detail', kwargs={'pk': self.pk})

admin.py

@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
    list_display = ('last_name', 'first_name', 'patronymic')
    search_fields = ['last_name']
    list_filter = ('profession')
    filter_horizontal = ('organizations', 'profession')

    def save_model(self, request, obj, form, change):
        obj.editor = request.user
        obj.save()

1 个答案:

答案 0 :(得分:0)

好的,通过将其添加到admin.py来修复它:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

(在此处找到:https://github.com/encode/django-rest-framework/issues/2912

不确定它是什么,但现在适合我。现在开始研究python中的编解码器。