我有以下名为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
答案 0 :(得分:1)
这很简陋。但是您可以将模型中的UnixTimestampField更改为DateTimeField。执行:
python manage.py schemamigration web --initial
在您将DateTimeField更改为UnixTimestampField
之后这必须有效....但这是一个肮脏的解决方案
虽然您的代码中可能存在错误,但请更改:
add_introspection_rules([], ["^web\customfields\.unixtimestampfield\.UnixTimestampField"])
为此:
add_introspection_rules([], ["^web\.customfields\.unixtimestampfield\.UnixTimestampField"])