草庐IT

python - Django South 错误 : AttributeError: 'DateTimeField' object has no attribute 'model' `

coder 2023-05-26 原文

所以我试图通过添加两列来迁移一个表。一个 startDate 和一个 endDate。对 Django 使用 south,这应该是一个简单的迁移。我还有很多其他带有 dateTimes 的表,但由于某种原因,我在这里得到并发布,但我没有看到它。

堆栈跟踪说明:

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() 来保持一切正常。问题是 South 的 schemamigration 工作得很好,但实际的迁移是错误的。

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

编辑: 包括 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'

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

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']"})
},

最佳答案

我必须将 djangosouth 版本升级到 0.8.4 版本。

必须运行以下命令:

sudo easy_install -U South

或者,如果使用 pip:

pip install South --upgrade

之后,我的迁移按预期进行。

关于python - Django South 错误 : AttributeError: 'DateTimeField' object has no attribute 'model' `,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20446724/

有关python - Django South 错误 : AttributeError: 'DateTimeField' object has no attribute 'model' `的更多相关文章

  1. ruby-on-rails - rails : keeping DRY with ActiveRecord models that share similar complex attributes - 2

    这似乎应该有一个直截了当的答案,但在Google上花了很多时间,所以我找不到它。这可能是缺少正确关键字的情况。在我的RoR应用程序中,我有几个模型共享一种特定类型的字符串属性,该属性具有特殊验证和其他功能。我能想到的最接近的类似示例是表示URL的字符串。这会导致模型中出现大量重复(甚至单元测试中会出现更多重复),但我不确定如何让它更DRY。我能想到几个可能的方向...按照“validates_url_format_of”插件,但这只会让验证干给这个特殊的字符串它自己的模型,但这看起来很像重溶液为这个特殊的字符串创建一个ruby​​类,但是我如何得到ActiveRecord关联这个类模型

  2. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  3. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  4. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  5. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  6. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  7. ruby - 在 jRuby 中使用 'fork' 生成进程的替代方案? - 2

    在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',

  8. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  9. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  10. ruby-on-rails - 新 Rails 项目 : 'bundle install' can't install rails in gemfile - 2

    我已经像这样安装了一个新的Rails项目:$railsnewsite它执行并到达:bundleinstall但是当它似乎尝试安装依赖项时我得到了这个错误Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcheckingforlibkern/OSAtomic.h...yescreatingMakefilemake"DESTDIR="cleanmake"DESTDIR="

随机推荐