2012-08-08 52 views
1

我呼吁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

回答

1

这是简陋。但是您可以将模型中的UnixTimestampField更改为DateTimeField。执行此:

python manage.py schemamigration web --initial 

而且当你改变了另一个时间DateTimeField字段来UnixTimestampField

这必须工作....但是这是肮脏的解决方案

Althought有可能是你在一个错误此

add_introspection_rules([], ["^web\customfields\.unixtimestampfield\.UnixTimestampField"]) 

:你的代码,更改此

add_introspection_rules([], ["^web\.customfields\.unixtimestampfield\.UnixTimestampField"]) 
+0

你说得对,它的路径是'web \ .customfields \ ...'。我注意到并修复了它,然后我看到了你的回应:)。尽管如此。 – KVISH 2012-08-08 17:46:05