2013-12-07 53 views
18

所以我試圖通過向它添加兩列來遷移表。 A startDateendDate。對於Django使用south,這應該是一個簡單的遷移。我也有大量的其他表中有dateTimes的表格,但由於某種原因,我得到和問題在這裏,我沒有看到它。Django South錯誤:AttributeError:'DateTimeField'對象沒有'model'的屬性

的堆棧跟蹤,指出:

AttributeError: 'DateTimeField' object has no attribute 'model' 

這裏是我的遷移模型:

# Keep track of who has applied for a Job 
class JobApply(models.Model): 
    job = models.ForeignKey(Jobs) 
    user = models.ForeignKey(User) 
    # Keep track of the Developer accepted to do the work 
    accepted_dev = models.IntegerField(null=False, blank=False, default=0) 
    # If 1 (True) the User has applied to this job 
    isApplied = models.BooleanField(default=0) 
    startDate = models.DateTimeField() 
    endDate = models.DateTimeField() 

所有除startDateendDate領域在數據庫中已經存在。因此,爲了給這些列默認值,我使用datetime.date.now()通過終端來保持一切正常。問題是,南的schemamigration工作得很好,但實際的遷移barfs。

如果有人能看到這個錯誤,我的頭髮會很感激。 :P

編輯: 包括堆棧跟蹤:

Running migrations for insource: 
- Migrating forwards to 0004_auto__add_field_jobapply_startDate__add_field_jobapply_endDate. 
> insource:0004_auto__add_field_jobapply_startDate__add_field_jobapply_endDate 
Error in migration: insource:0004_auto__add_field_jobapply_startDate__add_field_jobapply_endDate 
Traceback (most recent call last): 
    File "./manage.py", line 10, in <module> 
    execute_from_command_line(sys.argv) 
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 399, in execute_from_command_line 
    utility.execute() 
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 392, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 242, in run_from_argv 
    self.execute(*args, **options.__dict__) 
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 285, in execute 
    output = self.handle(*args, **options) 
    File "/usr/local/lib/python2.7/dist-packages/south/management/commands/migrate.py", line 111, in handle 
    ignore_ghosts = ignore_ghosts, 
    File "/usr/local/lib/python2.7/dist-packages/south/migration/__init__.py", line 220, in migrate_app 
    success = migrator.migrate_many(target, workplan, database) 
    File "/usr/local/lib/python2.7/dist-packages/south/migration/migrators.py", line 229, in migrate_many 
    result = migrator.__class__.migrate_many(migrator, target, migrations, database) 
    File "/usr/local/lib/python2.7/dist-packages/south/migration/migrators.py", line 304, in migrate_many 
    result = self.migrate(migration, database) 
    File "/usr/local/lib/python2.7/dist-packages/south/migration/migrators.py", line 129, in migrate 
    result = self.run(migration, database) 
    File "/usr/local/lib/python2.7/dist-packages/south/migration/migrators.py", line 113, in run 
    return self.run_migration(migration, database) 
    File "/usr/local/lib/python2.7/dist-packages/south/migration/migrators.py", line 83, in run_migration 
    migration_function() 
    File "/usr/local/lib/python2.7/dist-packages/south/migration/migrators.py", line 59, in <lambda> 
    return (lambda: direction(orm)) 
    File "/home/jared/Desktop/School/insource/insource/migrations/0004_auto__add_field_jobapply_startDate__add_field_jobapply_endDate.py", line 14, in forwards 
    keep_default=False) 
    File "/usr/local/lib/python2.7/dist-packages/south/db/generic.py", line 47, in _cache_clear 
    return func(self, table, *args, **opts) 
    File "/usr/local/lib/python2.7/dist-packages/south/db/generic.py", line 411, in add_column 
    sql = self.column_sql(table_name, name, field) 
    File "/usr/local/lib/python2.7/dist-packages/south/db/generic.py", line 706, in column_sql 
    default = field.get_db_prep_save(default, connection=self._get_connection()) 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 350, in get_db_prep_save 
    prepared=False) 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 911, in get_db_prep_value 
    value = self.get_prep_value(value) 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 902, in get_prep_value 
    (self.model.__name__, self.name, value), 
AttributeError: 'DateTimeField' object has no attribute 'model' 

遷移代碼(添加,因爲它是一個有點長相關代碼):

def forwards(self, orm): 
    # Adding field 'JobApply.startDate' 
    db.add_column(u'insource_jobapply', 'startDate', 
        self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime(2013, 12, 7, 0, 0)), 
        keep_default=False) 

    # Adding field 'JobApply.endDate' 
    db.add_column(u'insource_jobapply', 'endDate', 
        self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime(2013, 12, 7, 0, 0)), 
        keep_default=False) 


def backwards(self, orm): 
    # Deleting field 'JobApply.startDate' 
    db.delete_column(u'insource_jobapply', 'startDate') 

    # Deleting field 'JobApply.endDate' 
    db.delete_column(u'insource_jobapply', 'endDate') 


u'insource.jobapply': { 
    'Meta': {'object_name': 'JobApply'}, 
    'accepted_dev': ('django.db.models.fields.IntegerField', [], {'default': '0'}), 
    'endDate': ('django.db.models.fields.DateTimeField', [], {}), 
    u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 
    'isApplied': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), 
    'job': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['insource.Jobs']"}), 
    'startDate': ('django.db.models.fields.DateTimeField', [], {}), 
    'user': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']"}) 
}, 
+0

你能給我們整個堆棧跟蹤嗎?我沒有在任何地方看到'.model',因爲我想用'AttributeError' ... – mgilson

+0

您還可以共享一個遷移代碼嗎?我多次使用這種遷移,他們工作順利... –

+0

看到我編輯的問題。 – jaredgilmore

回答

30

我不得不升級我的版本的southdjango至版本0.8.4

只好運行以下命令:

sudo easy_install -U South 

或者,如果使用pip

pip install South --upgrade 

在那之後,我的移民都按預期。

+1

爲了完整起見:看起來這是在0.8.3中修復的:https://code.djangoproject.com/ticket/21312 – steps

相關問題