django
django自定义用户模型
Django最方便的一点可以说就是自带的用户系统了,不过某些情况下自带的用户系统不太符合项目需求,比如你想添加几个字段怎么办?当然可以使用自定Model然后外键关联User类来实现,不过一方面关联查询的效率比直接查询效率要低,另一方面想删除系统自带用户系统的某些字段怎么办呢?
所以,自定义用户模型可以说是一种很常见的需求。这里以Django1.9为例,记录一下自定义用户模型的方法。
Django-REST-framework中文文档教程
解决django使用logging时“character mapping must return integer, None or unicode”错误
今天调试Django项目时候,使用Logging记录异常并发送邮件给网站管理人员,测试时候始终报错:
Traceback (most recent call last):
File "/usr/lib64/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/home/xsy/.virtualenvs/dz_pro/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 177, in __call__
response = self.get_response(request)
File "/home/xsy/.virtualenvs/dz_pro/lib/python2.7/site-packages/django/core/handlers/base.py", line 230, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/home/xsy/.virtualenvs/dz_pro/lib/python2.7/site-packages/django/core/handlers/base.py", line 284, in handle_uncaught_exception
'request': request
File "/usr/lib64/python2.7/logging/__init__.py", line 1185, in error
self._log(ERROR, msg, args, **kwargs)
File "/usr/lib64/python2.7/logging/__init__.py", line 1278, in _log
self.handle(record)
File "/usr/lib64/python2.7/logging/__init__.py", line 1288, in handle
self.callHandlers(record)
File "/usr/lib64/python2.7/logging/__init__.py", line 1328, in callHandlers
hdlr.handle(record)
File "/usr/lib64/python2.7/logging/__init__.py", line 751, in handle
self.emit(record)
File "/home/xsy/.virtualenvs/dz_pro/lib/python2.7/site-packages/django/utils/log.py", line 117, in emit
self.send_mail(subject, message, fail_silently=True, html_message=html_message)
File "/home/xsy/.virtualenvs/dz_pro/lib/python2.7/site-packages/django/utils/log.py", line 120, in send_mail
mail.mail_admins(subject, message, *args, connection=self.connection(), **kwargs)
File "/home/xsy/.virtualenvs/dz_pro/lib/python2.7/site-packages/django/core/mail/__init__.py", line 97, in mail_admins
mail.send(fail_silently=fail_silently)
File "/home/xsy/.virtualenvs/dz_pro/lib/python2.7/site-packages/django/core/mail/message.py", line 292, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/xsy/.virtualenvs/dz_pro/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 100, in send_messages
new_conn_created = self.open()
File "/home/xsy/.virtualenvs/dz_pro/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 67, in open
self.connection.login(self.username, self.password)
File "/usr/lib64/python2.7/smtplib.py", line 607, in login
(code, resp) = self.docmd(encode_cram_md5(resp, user, password))
File "/usr/lib64/python2.7/smtplib.py", line 571, in encode_cram_md5
response = user + " " + hmac.HMAC(password, challenge).hexdigest()
File "/usr/lib64/python2.7/hmac.py", line 78, in __init__
self.outer.update(key.translate(trans_5C))
TypeError: character mapping must return integer, None or unicode
根据错误提示,追进了hmac.py中看了源码,发现key其实就是一个字符串,是配置在settings中的EMAIL_HOST_USER以及EMAIL_HOST_PASSWORD,而translate函数则是根据给出的参数表将字符串进行映射加密的,而且是最简单的凯撒加密法。
Django常用三方库
更新于2017.01.10
首先安利本书《two scoops of django1.8》目前还没有中文版,不过是我看过关于django的书中收获最大的一本。本来有机会和作者面基,可惜我大天朝的签证太难办了,只得作罢。
这里推荐三方库的大部分出自这本书,移除了部分失效或者不维护的库,添加了一些其他库,强烈建议大家阅读原书。
下面的模块有些是django模块,有些则是python模块,可以脱离django直接使用。
话说那些复制到别地方然后标注“原创”的,敢加个本文链接不?手工整理很累的好不?
eventlet,django,requests问题杂记
最近使用django+requests+eventlet做了个小程序,eventlet用来替代原生的多线程,最后发现有关manage.py的功能全都不能用了,报错信息类似:
django.db.utils.DatabaseError: DatabaseWrapper objects created in a thread can only be used in that same thread. The object with alias 'default' was created in thread id 139911009593152 and this is thread id 51055504.
产生这个问题的原因在于我的monky_patch()是在爬虫模块中执行,而希望这个爬虫模块能够保持独立性不希望和django融合的太深,所以解决问题只需根据实际需求在manage.py或settings.py或wsgi.py中先把monkey_patch()执行了即可。