Python 删除非ASCII字符的Django文件上载
我正在将文件名中包含unicode字符的文件上载到Django模型,但是当显示名称时,它们会被删除或更改为最接近的ascii字符。我检查了upload path函数,此时它仍然正确编码Python 删除非ASCII字符的Django文件上载,python,django,unicode,Python,Django,Unicode,我正在将文件名中包含unicode字符的文件上载到Django模型,但是当显示名称时,它们会被删除或更改为最接近的ascii字符。我检查了upload path函数,此时它仍然正确编码 -> return u'proposal_documents/{0}/{1}'.format(instance.proposal.pk, filename) (Pdb) x = u'proposal_documents/{0}/{1}'.format(instance.proposal.pk, filena
-> return u'proposal_documents/{0}/{1}'.format(instance.proposal.pk, filename)
(Pdb) x = u'proposal_documents/{0}/{1}'.format(instance.proposal.pk, filename)
(Pdb) x
u'proposal_documents/22872/\xa9\u02da\u2206\u222b\u02dau\u0308\u2122\xa2.png'
我还检查了文件的清理数据,文件也正确地表示在那里
-> for file_ in pdform.cleaned_data['files']:
(Pdb) pdform.cleaned_data['files']
[<InMemoryUploadedFile: ©˚∆∫˚ü™¢.png (image/png)>]
下面是模型(已删除import pdb;pdb.set_trace()
如何更改代码,使unicode字符保持不变?我正在使用Django 1.10和Python 2.7您能修复它吗?我从来没能修复它;这是Django内部的问题,我发现我的表排序错误,在将其更改为utf8\uUnicode\uCI之后,它工作正常。
(Pdb) models.ProposalDocument.objects.order_by('-created_at')
<QuerySet [<ProposalDocument: u.png>, ...
(Pdb) models.ProposalDocument.objects.order_by('-created_at').first().file.name
u'proposal_documents/22872/u.png'
def proposal_doc_upload_path(instance, filename):
return u'proposal_documents/{0}/{1}'.format(instance.proposal.pk, filename)
class ProposalDocument(models.Model):
proposal = models.ForeignKey(Proposal, on_delete=models.CASCADE, related_name='documents')
file = models.FileField(upload_to=proposal_doc_upload_path, max_length=500, blank=True)
created_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(Employee, on_delete=models.CASCADE)
def __str__(self):
return self.file.name.rsplit('/')[-1].encode('utf-8')