【问题标题】:How to delete models from django?如何从 django 中删除模型?
【发布时间】:2016-05-06 07:25:54
【问题描述】:

我想从数据库中删除一些表。早些时候,我有课程,部门之类的模型。现在,我想从数据库中删除所有这些并只使用一个:

class Student(models.Model):
    """here goes model for users"""

    def __str__(self):
        return self.name

    name = models.CharField(default = "",max_length=200)
    enrollment_no = models.CharField(default = "",max_length=10)
    batch = models.CharField(default = "",max_length=4)
    father_income = models.IntegerField(max_length=100)
    email = models.CharField(default = "",max_length=1000)
    mobile_number = models.CharField(default = "",max_length=1000)

但是,当我使用时:

python manage.py makemigrations

上面写着no changes detected. 并且在运行命令时:

python manage.py migrate

我正在将我的所有表都返回到数据库中,而这些表在 models.py 中不存在。我尝试使用:

  382  python manage.py makemigrations
  383  python manage.py squashmigrations
  384  python manage.py squashmigrations appname
  385  python manage.py squashmigrations appname 001
  386  python manage.py squashmigrations appname 0001
  387  python manage.py makemigrations 
  388  python manage.py migrate
  389  python manage.py Reporter.objects.all().delete()
  390  Reporter.objects.all().delete()
  391  python manage.py sqlclear appname | python manage.py dbshell
  392  python manage.py sqlclear appname
  393  ./manage.py sqlclear appname | ./manage.py dbshell
  394  python manage.py sqlclear
  395  python manage.py flush
  396  python manage.py syncdb
  397  python manage.py makemigrations
  398  python manage.py migrate
  399  python manage.py makemigrations
  400  python manage.py migrate
  401  python manage.py makemigrations
  402  python manage.py flush
  403  python manage.py makemigrations
  404  python manage.py migrate
  405  python manage.py makemigrations
  406  python manage.py migrate
  407  python manage.py makemigrations
  408  python manage.py migrate
  409  python manage.py sqlflush
  410  python manage.py sqlflush | python manage.py dbshell
  411  python manage.py sqlclear jmialumniusa_app
  412  python manage.py help flush
  413  python manage.py flush
  414  python manage.py makemigrations
  415  python manage.py migrate
  416  python manage.py schemamigration --auto appname/
  417  python manage.py schemamigration --auto appname
  418  python manage.py makemigrations
  419  history

但没有成功。请帮我解决这个问题。

python manage.py makemigrations jmialumniusa_app 的输出为:

System check identified some issues:

WARNINGS:
?: (urls.W001) Your URL pattern '^$' uses include with a regex ending with a '$'. Remove the dollar from the regex to avoid problems including URLs.
No changes detected in app 'jmialumniusa_app'

最后在python manage.py migrate 我得到了:

System check identified some issues:

WARNINGS:
?: (urls.W001) Your URL pattern '^$' uses include with a regex ending with a '$'. Remove the dollar from the regex to avoid problems including URLs.
Operations to perform:
  Apply all migrations: admin, contenttypes, sessions, auth, jmialumniusa_app
Running migrations:
  Rendering model states... DONE
  Applying jmialumniusa_app.0003_auto_20160506_0654...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 353, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 345, 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 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/fields.py", line 121, in database_forwards
    schema_editor.remove_field(from_model, from_model._meta.get_field(self.name))
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 438, in remove_field
    self.execute(sql)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 110, in execute
    cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 112, in execute
    return self.cursor.execute(query, args)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
django.db.utils.OperationalError: (1091, "Can't DROP 'student_id'; check that column/key exists")

【问题讨论】:

  • 警告是完全独立的,它告诉您您的网址格式存在问题。删除它所说的美元(即将url(r'^$', include(...))更改为url(r'^', include(...))

标签: python mysql django


【解决方案1】:

当我遇到同样的问题时,我通过这种方式解决了它:

  • 打开您的应用文件夹,然后打开迁移文件夹
  • 删除除init以外的所有文件
  • 尝试 makemigrations 并再次迁移

如果问题出在数据库中,而不是迁移中,这可能无济于事。

【讨论】:

    【解决方案2】:

    从 models.py 中删除不需要的模型后,试试这个:

    python manage.py makemigrations your_app
    python manage.py migrate
    

    原因可能是您的应用可能尚未“注册”以进行迁移

    【讨论】:

    • 从models.py中删除不需要的?或者,从 mysql 数据库手动?
    • python manage.py makemigrations jmialumniusa_app 系统检查发现了一些问题:警告:?:(urls.W001)您的 URL 模式“^$”使用包含以“$”结尾的正则表达式。从正则表达式中删除美元以避免包括 URL 在内的问题。在应用“应用”中未检测到任何更改
    • @learner 你在这个应用中有迁移文件吗?如果有,有多少?
    • 我只有一个迁移文件,models.py。由于我是 django 新手,请您指出迁移文件是什么?我假设它是models.py。
    • @learner 不,这不是models.py。您的应用中有迁移文件夹。除了初始文件,您在其中看到了什么?
    【解决方案3】:

    第一个答案应该适合你。 您可以做的另一件事是删除应用程序中的迁移文件夹并运行 python manage.py makemigrations app_name

    将自动创建一个新的迁移文件夹。 那么你就可以迁移了

    python manage.py migrate 在这之后你应该很好。 我不鼓励这样做,但作为最后的手段,如果您使用的是 sqlite,只需将其删除并执行我上面所说的操作。

    https://docs.djangoproject.com/en/1.9/topics/migrations/ 将让您更深入地了解迁移在 django 中的工作方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      • 2017-12-31
      • 1970-01-01
      • 2011-04-17
      相关资源
      最近更新 更多