2012-08-08 34 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