最近在用django做一下项目, 我也是第一次使用django, 也是第一次比较正式的使用Python. 本文将记录一下使用django过程中遇到的一下问题和解决办法. 并且不断更新!
setting设置
几天发现部署以后显示的时间不多, 是因为时区设置到问题
对于我们中国:
TIME_ZONE = 'Asia/Shanghai'
LANGUAGE_CODE = 'zh-cn'
ManyToMany的bug
如果你在使用many-to-many去取数据的时候发生如下异常:
TypeError: filter() keywords must be strings
这个是有django的bug引起的:
修复方法:
django\db\models\fields\related.py 349行
def __init__(self, model=None, core_filters=None, instance=None, symmetrical=None,
join_table=None, source_col_name=None, target_col_name=None):
super(ManyRelatedManager, self).__init__()
self.core_filters = core_filters
self.model = model
self.symmetrical = symmetrical
self.instance = instance
self.join_table = join_table
self.source_col_name = source_col_name
self.target_col_name = target_col_name
self._pk_val = self.instance._get_pk_val()
改成
def __init__(self, model=None, core_filters=None, instance=None, symmetrical=None,
join_table=None, source_col_name=None, target_col_name=None):
super(ManyRelatedManager, self).__init__()
for key, value in core_filters.items():
if not isinstance(key, str):
del core_filters[key]
core_filters[smart_str(key)] = value
self.core_filters = core_filters
self.model = model
self.symmetrical = symmetrical
self.instance = instance
self.join_table = join_table
self.source_col_name = source_col_name
self.target_col_name = target_col_name
self._pk_val = self.instance._get_pk_val()