learner learner - 2 years ago 417
Python Question

How to delete models from django?

I want to delete some tables from database. Earlier, I had models like course, department. Now, I want to delete all of them from database and use only one:

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)

But, when I am using:

python manage.py makemigrations

It says
no changes detected.
And on running command:

python manage.py migrate

I am getting all my tables back in the database, while those table doesn't exist in models.py. I tried to use:

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

but getting no success. Please help me to solve this.

The output of
python manage.py makemigrations jmialumniusa_app

System check identified some issues:

?: (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'

And finally on
python manage.py migrate
I am getting:

System check identified some issues:

?: (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>
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 345, in execute
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
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")

Answer Source

after deleting your unwanted models from models.py, try this:

python manage.py makemigrations your_app
python manage.py migrate

reason can be that your app might not have been yet "registered" to have migrations

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download