问题描述
所以我试图通过向其中添加两列来迁移表。 startDate
和 endDate
。使用 south
为Django,这应该是一个简单的迁移。我有很多其他表与dateTimes在其中,但是由于某些原因我得到并在这里发布,我看不到它。
堆栈跟踪说明:
AttributeError:'DateTimeField'对象没有属性'model'
以下是我正在迁移的模型:
#跟踪谁申请了一个Job
class JobApply(models.Model):
job = models.ForeignKey(Jobs)
user = models.ForeignKey(User)
#跟踪开发者接受的工作
accepted_dev = models.IntegerField(null = False,blank = False,default = 0)
#如果1(True)用户已应用于此作业
isApplied = models.BooleanField(default = 0)
startDate = models.DateTimeField()
endDate = models.DateTimeField()
除 startDate
和 endDate
之外的所有字段已经存在于DB中。所以为了给这些列默认值,我通过终端使用datetime.date.now()来保持一切正常。问题是南方的 schemamigration
工作正常,但实际的迁移barfs。
如果有人可以看到错误我的头发会很欣赏的。 :P
编辑 :
包括StackTrace:
为内容资源运行迁移:
- 向前迁移到0004_auto__add_field_jobapply_startDate__add_field_jobapply_endDate。
> insource:0004_auto__add_field_jobapply_startDate__add_field_jobapply_endDate
迁移错误:insource:0004_auto__add_field_jobapply_startDate__add_field_jobapply_endDate
追溯(最近的最后一次调用):
文件./manage.py,第10行在< module>
execute_from_command_line(sys.argv)
文件/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py,第399行,execute_from_command_line
utility.execute()
文件/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py,第392行,执行
self.fetch_command (子命令).run_from_argv(self.argv)
文件/usr/local/lib/python2.7/dist-packages/django/core/management/base.py,第242行,在run_from_argv
self.execute(* args,** options .__ dict__)
文件/usr/local/lib/python2.7/dist-packages/django/core/management/base.py,第285行,执行
output = self.handle(* args,** options)
文件/usr/local/lib/python2.7/dist-packages/south/management/commands/migrate.py,第111行,在句柄
ignore_ghosts = ignore_ghosts,
文件/usr/local/lib/python2.7/dist-packages/south/migration/__init__.py,第220行,在migrate_app
success = migrator.migrate_many(target,wor kplan,database)
文件/usr/local/lib/python2.7/dist-packages/south/migration/migrators.py,第229行,在migrate_many
result = migrator .__ class __。migrate_many (migrator,target,migrations,database)
文件/usr/local/lib/python2.7/dist-packages/south/migration/migrators.py,第304行,在migrate_many
result = self.migrate(migration,database)
文件/usr/local/lib/python2.7/dist-packages/south/migration/migrators.py,第129行,迁移
result = self .run(migration,database)
文件/usr/local/lib/python2.7/dist-packages/south/migration/migrators.py,第113行,运行
return self.run_migration (迁移,数据库)
文件/usr/local/lib/python2.7/dist-packages/south/migration/migrators.py,第83行,在run_migration
migration_function()
文件/usr/local/lib/python2.7/dist-packages/south/migration/migrators.py,第59行,< lambda>
return(lambda:direction(orm))
文件/home/jared/Desktop/School/insource/insource/migrations/0004_auto__add_field_jobapply_startDate__add_field_jobapply_endDate.py,第14行转发
keep_default = False)
文件/usr/local/lib/python2.7/dist-packages/south/db/generic.py,第47行,在_cache_clear
中返回func(self,table,* args ,** opts)
文件/usr/local/lib/python2.7/dist-packages/south/db/generic.py,第411行,add_column
sql = self.column_sql( table_name,name,field)
文件/usr/local/lib/python2.7/dist-packages/south/db/generic.py,第706行,column_sql
default = field.get_db_prep_save (默认,connection = self._get_connection())
文件/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py,第350行,在get_db_prep_save
prepared = False)
文件/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py,第911行,get_db_prep_value
值= self.get_prep_value(value)
文件/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py,第902行,在get_prep_value
(self.model .__ name__,self.name,value),
AttributeError:'DateTimeField'对象没有属性'model'
迁移代码(添加相关代码,因为它有点长):
def forward(self ,orm):
#添加字段'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)
#添加字段'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):
#删除字段'JobApply.startDate'
db.delete_column(u'insource_jobapply','startDate')
#删除字段'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':uorm ['insource.Jobs'])),
'startDate':('django.db .models.fields.DateTimeField',[],{}),
'user':('django.db.model s.fields.related.ForeignKey',[],{'to':uorm ['auth.User']})
},
我不得不升级我的版本 south
$ c> django 到版本 0.8.4
。
必须运行以下命令:
sudo easy_install -U南
或者,如果使用 pip
:
pip install South --upgrade
之后,我的迁移按预期工作。 >
So I'm trying to migrate a table by adding two columns to it. A startDate
and an endDate
. Using south
for Django, this should be a simple migrate. I have loads of other tables with dateTimes in them as well, but for some reason I'm getting and issue here and I don't see it.
The stack trace is stating:
AttributeError: 'DateTimeField' object has no attribute 'model'
Here is the model I am migrating:
# 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()
All the fields except for startDate
and endDate
already exist in the DB. So to give those columns default values I use datetime.date.now() through the terminal to keep everything square. The issue is that South's schemamigration
works just fine, but the actual migration barfs.
If anyone can see the error, my hair would appreciate it. :P
EDIT:Including Stacktrace:
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'
Migration Code (adding relevant code as it's a bit long):
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']"})
},
I had to upgrade my version of south
for django
to version 0.8.4
.
Had to run the following command:
sudo easy_install -U South
Or, if using pip
:
pip install South --upgrade
After that, my migration worked as expected.
这篇关于Django South错误:AttributeError:'DateTimeField'对象没有属性'model'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!