我有以下名为UnixTimestampField的类:
from django.db import models
from datetime import datetime
from time import strftime
class UnixTimestampField(models.DateTimeField):
op_params=''
def __init__(self, null=False, blank=False, op_params='', **kwargs):
super(UnixTimestampField, self).__init__(**kwargs)
self.blank, self.isnull = blank, null
self.null = True
def db_type(self, connection):
typ=['TIMESTAMP']
# See above!
if self.isnull:
typ += ['NULL']
if self.op_params != '':
typ += [self.op_params]
return ' '.join(typ)
def to_python(self, value):
return datetime.from_timestamp(value)
def get_db_prep_value(self, value, connection, prepared=False):
if value==None:
return None
return strftime('%Y%m%d%H%M%S',value.timetuple())
def to_python(self, value):
return value
from south.modelsinspector import add_introspection_rules
add_introspection_rules([], ["^web\customfields\.unixtimestampfield\.UnixTimestampField"])
每次我运行以下命令:
python manage.py schemamigration web --initial
,我都会不断得到:! (this field has class web.customfields.unixtimestampfield.UnixTimestampField)
有什么我想念的吗?似乎甚至不知道该字段存在吗?我在以下位置阅读文档:
http://south.readthedocs.org/en/latest/customfields.html#extending-introspection
http://south.readthedocs.org/en/latest/tutorial/part4.html#keyword-arguments
[解]
该错误是一个简单的错误。
下一行:
^web\customfields\.unixtimestampfield\.UnixTimestampField
不正确。改为:
^web\.customfields\.unixtimestampfield\.UnixTimestampField
最佳答案
好吃但是您可以将模型中的UnixTimestampField更改为DateTimeField。执行此:
python manage.py schemamigration web --initial
在更改了另一个时间之后,将DateTimeField更改为UnixTimestampField
这必须工作....但这是肮脏的解决方案
尽管您的代码中可能存在错误,但是请更改此代码:
add_introspection_rules([], ["^web\customfields\.unixtimestampfield\.UnixTimestampField"])
为了这:
add_introspection_rules([], ["^web\.customfields\.unixtimestampfield\.UnixTimestampField"])
关于python - 南方不认识我的模型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11869504/