首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Django South错误:模型:'DateTimeField‘对象没有属性’AttributeError‘`

Django South错误:模型:'DateTimeField‘对象没有属性’AttributeError‘`
EN

Stack Overflow用户
提问于 2013-12-08 05:14:35
回答 1查看 4.2K关注 0票数 18

因此,我正在尝试通过向表中添加两列来迁移表。一个startDate和一个endDate。使用south for Django,这应该是一个简单的迁移。我还有很多包含dateTimes的其他表,但是出于某种原因,我在这里得到并发布了,但我没有看到它。

堆栈跟踪表明:

代码语言:javascript
复制
AttributeError: 'DateTimeField' object has no attribute 'model'

下面是我正在迁移的模型:

代码语言:javascript
复制
# 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运行得很好,但实际的迁移情况却很糟糕。

如果有人能看到这个错误,我的头发会很感激的。:P

编辑:包括堆栈跟踪:

代码语言:javascript
复制
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'

迁移代码(添加相关代码,因为有点长):

代码语言:javascript
复制
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']"})
},
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-09 04:20:36

我不得不将我的djangosouth升级到0.8.4版。

我必须运行以下命令:

代码语言:javascript
复制
sudo easy_install -U South

或者,如果使用pip

代码语言:javascript
复制
pip install South --upgrade

在那之后,我的迁移就像预期的那样。

票数 30
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20446724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档