Skip to content

Marina Mele's site

Reflections on family, values, and personal growth

Menu
  • Home
  • About
Menu

How to correctly remove a Model or an App in Django

Posted on October 28, 2014October 28, 2014 by Marina Mele

When you remove a Django Model or a full app in Django, you have to be careful in how you do it, and how the database will be affected.

Thus, in this short post I’ll explain what I do when deleting a model or an app.

Remove the Models from models.py

Weather you’re removing a single model or a full app, you should first remove the desired models from the models.py file.

Moreover, you have to make sure that no other file imports these models or uses them (admin.py, views.py, etc).

Next, run South or migrate your database to properly delete these models from your database.

If you are using South, you should run something like

$ python manage.py schemamigration --auto your-app
$ python manage.py migrate your-app

changing your-app by the name of the app to migrate (where your deleted models were).

Remove an App from your Django project

Next, if you are removing an entire app, you can now remove all the files in that app (models.py, views.py, etc). Again, make sure that the other apps don’t make an import from these files.

And finally, remove this app from your Installed Apps in the settings.py file.

Remove the remnant ContentTypes

But after doing all this, you will still have remnants of your models in the ContentType table, like permissions related to the deleted models.

One way to see this is when creating a Group in the Auth app using the admin. When you have to choose the permissions of the group, the box will show permissions that refer to the deleted models.

To clean these tables, we will create the file clean_model_app_remove.py and write the following:

from django.contrib.contenttypes.models import ContentType

# List of deleted apps
DEL_APPS = ["app-you-deleted", "second-app-deleted"]
# List of deleted models (that are not in the app deleted) In lowercase!
DEL_MODELS = ["model-you-deleted", "second-model-deleted"]

ct = ContentType.objects.all().order_by("app_label", "model")

for c in ct:
    if (c.app_label in DEL_APPS) or (c.model in DEL_MODELS):
        print "Deleting Content Type %s %s" % (c.app_label, c.model)
        c.delete()

where you should write in DEL_APPS the apps that you deleted, and in DEL_MODELS the models that you delete and that do not belong to any of the deleted apps. Moreover, note that the models have to be written in lowercase.

Finally, we just need to run our script with

$ python manage.py shell
>>>execfile("clean_model_app_remove.py")

Great! Now we cleanly removed our apps/models! 🙂

Marina Melé
Marina Mele

Marina Mele has experience in artificial intelligence implementation and has led tech teams for over a decade. On her personal blog (marinamele.com), she writes about personal growth, family values, AI, and other topics she’s passionate about. Marina also publishes a weekly AI newsletter featuring the latest advancements and innovations in the field (marinamele.substack.com)

Leave a Reply Cancel reply

You must be logged in to post a comment.

Categories

  • Personal Growth and Development
  • Artificial Intelligence
  • Mindful Parenting and Family Life
  • Productivity and Time Management
  • Mindfulness and Wellness
  • Values and Life Lessons
  • Posts en català
  • Other things to learn

Recent Posts

  • Understanding Frustration in Children
  • What is ChatGPT and how it compares to Bard and Claude
  • BlueSky Social – A Sneak Peek at the Future of Social Media
  • The Incredible Journey of AI Image Generation
  • AI and Fundamental Rights: How the AI Act Aims to Protect Individuals

RSS

  • Entries RSS
Follow @marina_mele
  • Cookie Policy
  • Privacy Policy
©2025 Marina Mele's site | Built using WordPress and Responsive Blogily theme by Superb
This website uses cookies to improve your experience. If you keep navigating through this website, we'll assume you're ok with this, but you can opt-out if you wish.Accept Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT