Python论坛  - 讨论区

标题:[python-chinese] 关于django的DB filter和unicode

2007年07月20日 星期五 15:58

Tao Guan jack139在gmail.com
星期五 七月 20 15:58:20 HKT 2007

今天折腾一下午,总算有个临时的结果,分享一下。另外各位还有什么好的方法,赐教一下。

环境:django 0.96+apache+mysql
charset: 全部是 utf-8

models定义:
class Tag(models.Model):
    name = models.CharField(maxlength=40, unique=True)

判断name是否已存在的func:
def _is_tag_exist(s):
    return Tag.objects.filter(name__iexact=s.strip())

_is_tag_exist对英文没的说,问题是中文,当中文大于等于14个字时,就查不到了。而13个汉字的name就可以查到。用encode()
decode()弄了半天都搞不定。最后只好上赤裸裸的SQL了。

def _is_tag_exist(s):
    return Tag.objects.all().extra(where=['name="' + s.strip() +'"'])

这个就搞定了。

小结:因为mysql是支持utf-8的,所以name里放40个汉字是没问题的,只是用filter()查询不到大于13个汉字的。好象因为gbk是三字节编码,14个字就是42字节,大于maxlength=40。可能django在处理filter时在哪里用maxlength给限制住了。俺是python新鸟,看了半天django的filter()实现,,,没看懂。:p
查了django的文档,开发版本好象已经全面支持unicode了,0.96里还没有。不知道的是否有改善。

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年07月20日 星期五 16:05

wang_zheng_yong wang_zheng_yong在163.com
星期五 七月 20 16:05:19 HKT 2007

gbkÊÇÈý×Ö½Ú±àÂë  ????????????????


ÊÇÈý×Ö½ÚÂð£¿

Á½×Ö½Ú°É



wang_zheng_yong
2007-07-20



·¢¼þÈË£º Tao Guan
·¢ËÍʱ¼ä£º 2007-07-20 15:58:32
ÊÕ¼þÈË£º python-chinese在lists.python.cn
³­ËÍ£º 
Ö÷Ì⣺ [python-chinese] ¹ØÓÚdjangoµÄDB filterºÍunicode

½ñÌìÕÛÌÚÒ»ÏÂÎ磬×ÜËãÓиöÁÙʱµÄ½á¹û£¬·ÖÏíһϡ£ÁíÍâ¸÷λ»¹ÓÐʲôºÃµÄ·½·¨£¬´Í½Ìһϡ£

»·¾³£ºdjango 0.96+apache+mysql
charset: È«²¿ÊÇ utf-8

models¶¨Ò壺
class Tag(models.Model):
    name = models.CharField(maxlength=40, unique=True)

ÅжÏnameÊÇ·ñÒÑ´æÔÚµÄfunc:
def _is_tag_exist(s):
    return Tag.objects.filter(name__iexact=s.strip())

_is_tag_exist¶ÔÓ¢ÎÄûµÄ˵£¬ÎÊÌâÊÇÖÐÎÄ£¬µ±ÖÐÎÄ´óÓÚµÈÓÚ14¸ö×Öʱ£¬¾Í²é²»µ½ÁË¡£¶ø13¸öºº×ÖµÄname¾Í¿ÉÒԲ鵽¡£ÓÃencode()
decode()ŪÁË°ëÌ춼¸ã²»¶¨¡£×îºóÖ»ºÃÉϳàÂãÂãµÄSQLÁË¡£

def _is_tag_exist(s):
    return Tag.objects.all().extra(where=['name="' + s.strip() +'"'])

Õâ¸ö¾Í¸ã¶¨ÁË¡£

С½á£ºÒòΪmysqlÊÇÖ§³Öutf-8µÄ£¬ËùÒÔnameÀï·Å40¸öºº×ÖÊÇûÎÊÌâµÄ£¬Ö»ÊÇÓÃfilter()²éѯ²»µ½´óÓÚ13¸öºº×ֵġ£ºÃÏóÒòΪgbkÊÇÈý×Ö½Ú±àÂ룬14¸ö×Ö¾ÍÊÇ42×Ö½Ú£¬´óÓÚmaxlength=40¡£¿ÉÄÜdjangoÔÚ´¦ÀífilterʱÔÚÄÄÀïÓÃmaxlength¸øÏÞÖÆסÁË¡£°³ÊÇpythonÐÂÄñ£¬¿´ÁË°ëÌìdjangoµÄfilter()ʵÏÖ£¬£¬£¬Ã»¿´¶®¡£:p
²éÁËdjangoµÄÎĵµ£¬¿ª·¢°æ±¾ºÃÏóÒѾ­È«ÃæÖ§³ÖunicodeÁË£¬0.96ÀﻹûÓС£²»ÖªµÀµÄÊÇ·ñÓиÄÉÆ¡£
_______________________________________________
python-chinese
Post: send python-chinese在lists.python.cn
Subscribe: send subscribe to python-chinese-request在lists.python.cn
Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
Detail Info: http://python.cn/mailman/listinfo/python-chinese
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070720/fb451faf/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年07月20日 星期五 16:54

haur hekun06在gmail.com
星期五 七月 20 16:54:37 HKT 2007

def _is_tag_exist(s):
   return Tag.objects.all().extra(where=['name="' + s.strip() +'"'])
д·¨²»´í¹þ,ÄÇÀïÓÐÕâÑùµÄд·¨½éÉÜÑÈ ?

ÔÚ07-7-20£¬Tao Guan <jack139在gmail.com> дµÀ£º
>
> ½ñÌìÕÛÌÚÒ»ÏÂÎ磬×ÜËãÓиöÁÙʱµÄ½á¹û£¬·ÖÏíһϡ£ÁíÍâ¸÷λ»¹ÓÐʲôºÃµÄ·½·¨£¬´Í½Ìһϡ£
>
> »·¾³£ºdjango 0.96+apache+mysql
> charset: È«²¿ÊÇ utf-8
>
> models¶¨Ò壺
> class Tag(models.Model):
>     name = models.CharField(maxlength=40, unique=True)
>
> ÅжÏnameÊÇ·ñÒÑ´æÔÚµÄfunc:
> def _is_tag_exist(s):
>     return Tag.objects.filter(name__iexact=s.strip())
>
> _is_tag_exist¶ÔÓ¢ÎÄûµÄ˵£¬ÎÊÌâÊÇÖÐÎÄ£¬µ±ÖÐÎÄ´óÓÚµÈÓÚ14¸ö×Öʱ£¬¾Í²é²»µ½ÁË¡£¶ø13¸öºº×ÖµÄname¾Í¿ÉÒԲ鵽¡£ÓÃencode()
> decode()ŪÁË°ëÌ춼¸ã²»¶¨¡£×îºóÖ»ºÃÉϳàÂãÂãµÄSQLÁË¡£
>
> def _is_tag_exist(s):
>     return Tag.objects.all().extra(where=['name="' + s.strip() +'"'])
>
> Õâ¸ö¾Í¸ã¶¨ÁË¡£
>
>
> С½á£ºÒòΪmysqlÊÇÖ§³Öutf-8µÄ£¬ËùÒÔnameÀï·Å40¸öºº×ÖÊÇûÎÊÌâµÄ£¬Ö»ÊÇÓÃfilter()²éѯ²»µ½´óÓÚ13¸öºº×ֵġ£ºÃÏóÒòΪgbkÊÇÈý×Ö½Ú±àÂ룬14¸ö×Ö¾ÍÊÇ42×Ö½Ú£¬´óÓÚmaxlength=40¡£¿ÉÄÜdjangoÔÚ´¦ÀífilterʱÔÚÄÄÀïÓÃmaxlength¸øÏÞÖÆסÁË¡£°³ÊÇpythonÐÂÄñ£¬¿´ÁË°ëÌìdjangoµÄfilter()ʵÏÖ£¬£¬£¬Ã»¿´¶®¡£:p
> ²éÁËdjangoµÄÎĵµ£¬¿ª·¢°æ±¾ºÃÏóÒѾ­È«ÃæÖ§³ÖunicodeÁË£¬0.96ÀﻹûÓС£²»ÖªµÀµÄÊÇ·ñÓиÄÉÆ¡£
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070720/e39319b9/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年07月21日 星期六 08:42

 
星期六 七月 21 08:42:58 HKT 2007

_______________________________________________
python-chinese
Post: sendpython-chinese在lists.python.cn
Subscribe: send subscribe topython-chinese-request在lists.python.cn
Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
Detail Info:http://python.cn/mailman/listinfo/python-chinese
_______________________________________________
python-chinese
Post: sendpython-chinese在lists.python.cn
Subscribe: send subscribe topython-chinese-request在lists.python.cn
Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
Detail Info:http://python.cn/mailman/listinfo/python-chinese
_______________________________________________
python-chinese
Post: sendpython-chinese在lists.python.cn
Subscribe: send subscribe topython-chinese-request在lists.python.cn
Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
Detail Info:http://python.cn/mailman/listinfo/python-chinese
------=_Part_140500_6571563.1184979241183
Content-Type: text/html; charset=gbk
Content-Transfer-Encoding: quoted-printable

=C4=E3=BF=B4=B5=C4=BC=DB=B8=F1=CA=C7=B8=D5=B8=D5=BD=B5=CF=C2=C8=A5=B5= =C4=BC=DB=B8=F1=A1=A3=B5=B1=B5=B1=D5=E2=B1=BE=CA=E9=B8=D5=C9=CF=BC=DC=CA=B1= =CA=C795=D5=DB=A1=A3
=CF=D6=D4=DA=B5=B1=B5=B1=A1=A2=D7=BF=D4=BD=B5=C4=BC=DB=B8=F1=B6=BC=BD= =B5=B5=BD75=D5=DB=D7=F3=D3=D2=C1=CB=A3=AC=D7=BF=D4=BD=B9=F38=C3=AB=C7=AE=A3= =AC=B5=AB=C3=E2=C5=E4=CB=CD=B7=D1=A1=A3
=D4=DA=B5=B1=B5=B1=C2=F2=CA=E9=B4=D325=BA=C5=BF=C9=D2=D4=D3=C35=D4=AA= =C8=AF=A3=AC=B1=F0=CD=FC=C1=CB=A1=A3XDF2007

=D4=DA2007-07-20=A3=AC"magic lu" <lujianxy在gmail.com>= =D0=B4=B5=C0=A3=BA
=D2=BB=D1=F9=B5=C4=BC=DB=B8=F1=B0=C9=A3=BF
=B6=BC=CA=C7=C6=D5=CD=A8=D5=DB=BF=DB=BC=DB=CE=AA51

 
On 7/20/07, hong= bozhang <hbzhang.1983在gmail.com">hbzhang.1983在gmai= l.com> wrote:=20
china-pub=B5=C4=BC=DB=B8=F1=B8=FA=B5=B1=B5=B1=B2=EE=BE=E0=D2=B2=CC=AB= =B4=F3=C1=CB=B0=C9=A3=A1
=C2=D2=BC=DB

 
2007/7/20, klutz chump <chumpklutz在gmail.= com" target=3D_blank>chumpklutz在gmail.com>:=20
china-pub=D1e=C3=E6=C6=E4=8C=8D=D2=B2=CA=C7=BF=C9=D2=D4=C3=E2=CB=CD=D8= =9B=D9M=B5=C4=A3=AC=CE=FB=CE=FB~~=A1=A1=D0=E8=D2=AA=D2=BB=D0=A9=BC=BC=C7=C9= =B5=C4
 
 
=D3=C9=EC=B6china-pub=CA=C7=B8=DF=D0=A3=C3=E2=D9M=CB=CD=D8=9B=A3=AC=CB= =F9=D2=D4=D2=AA=DFx=93=F1=D2=BB=82=80=B4=F3=8CW=A3=AC=C8=BB=E1=E1=D4=DA=B5= =D8=D6=B7=D7=EE=E1=E1=BC=D3=C9=CF=A3=A8XX=B4=F3=8CW=A3=A9
=DF@=98=D3=BE=CD=BF=C9=D2=D4=C3=E2=D9M=CB=CD=D8=9B=C1=CB~~

&nbs;= p;
=D4=DA07-7-20=A3=ACQi Ivan <qihboy在gmail.com" target=3D_blank>qihboy在gmail.com= > =D0=B4=B5=C0=A3=BA


On 7/20/07, beck= 917 <beck917在gmail.com" target=3D_blank>beck917在gmail.com> wr= ote:
=D5=E2=B1=BE=CA=E9=B2=BB=B4=ED=A3= =AC=B4=F2=CB=E3=C8=EB=CA=D6=A3=AC=C2=A5=C9=CF=B5=C4=B4=ED=B9=FD=BF=C9=CF=A7= =C1=CB

=CE=D2=CF=C8=C8=A5CSDN=BF=B4=D2=BB=CF=C2=CA=D4=B6=C1=B5=C4,=D4=D9=C8= =A5=C2=F2=B2=BB=B3=D9.
=D4=DA=B4=F3=C1=AC=B8=E3PYTHON=B5=C4=C8=CB=BB=B9=CA=C7=B7=C7=B3=A3=C9= =D9=B5=C4,=CB=E4=C8=BB=C5=BC=CF=D6=D4=DA=B9=A4=D7=F7=D2=B2=B2=BB=CA=C7=B8= =E3PYTHON
=C8=A8=B5=B1=B8=F6=C8=CB=B0=AE=BA=C3=C1=CB.

--
Best Regards
From IvanQi

__________= _____________________________________
python-chinese
Post: send n-chinese在lists.python.cn" target=3D_blank>python-chinese在lists.python.cn
Subscribe: send subscribe to python-chinese-request在lists.python.cn" = target=3D_blank>python-chinese-request在lists.python.cn
Unsubscribe:= send unsubscribe to  python-chinese-request在lists.python.cn" target= =3D_blank>python-chinese-request在lists.python.cn
Detail Info: http://pytho= n.cn/mailman/listinfo/python-chinese" target=3D_blank>http://python.cn/mail= man/listinfo/python-chinese


______= _________________________________________
python-chinese
Post: send = python-chinese在lists.python.cn" target=3D_blank>python-chinese在lists.python= .cn
Subscribe: send subscribe to python-chinese-request在lists.pytho= n.cn" target=3D_blank>python-chinese-request在lists.python.cn
Unsubsc= ribe: send unsubscribe to   python-chinese-request在lists.python.cn" ta= rget=3D_blank>python-chinese-request在lists.python.cn
Detail Info: <= A onclick=3D"return top.js.OpenExtLink(window,event,this)" href=3D"http://p= ython.cn/mailman/listinfo/python-chinese" target=3D_blank>http://python.cn/= mailman/listinfo/python-chinese

=
_______________________________________________
python-chinese
Po= st: send python-chinese在lists.python.cn">python-chinese在lists.python.cn <= /A>
Subscribe: send subscribe to python-chinese-request在lists.python.cn"= >python-chinese-request在lists.python.cn
Unsubscribe: send unsubscrib=
e to   python-chinese-request在lists.python.cn">python-chinese-request@= lists.python.cn
Detail Info: http://python.cn/mailman/listinfo/python-chine= se" target=3D_blank>http://python.cn/mailman/listinfo/python-chinese>




http://event.mail.163.com/chanel/xyq.htm?from=3D163_NO7" target= =3D"_blank">=BE=A2 = =B1=AC 150 =CD=F2 =CD=AC =CA=B1 =D4=DA =CF=DF=A3=AC=D6=DA =C8=CB =D7=B7 =C5= =F5 =C3=CE =BB=C3 =CE=F7 =D3=CE ------=_Part_140500_6571563.1184979241183--

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号