30👍
If you create a new app manually and add it to the INSTALLED_APPS setting without adding a migrations module inside it, the system will not pick up changes as this is not considered a migrations configured app.
The startapp command automatically adds the migrations module inside of your new app.
startapp structure
foo/
__init__.py
admin.py
models.py
migrations/
__init__.py
tests.py
views.py
1👍
There are a few steps necessary for Django to pick up the newly created app and the models created inside it.
Let’s assume you created a new app called messaging
with the command python manage.py startapp messaging
or by manually adding the following directory structure:
- project_name
- messaging
- migrations
- __init__.py
- __init__.py
- admin.py
- apps.py
- models.py
- tests.py
- views.py
- project_name
- __init__.py
- settings.py
- urls.py
- wsgi.py
- manage.py
In settings.py
you need to add the app to INSTALLED_APPS
like this:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'messaging.apps.MessagingConfig'
]
After adding models to models.py
, e.g:
from django.contrib.auth.models import User
from django.db import models
from django.db.models import DO_NOTHING
class Thread(models.Model):
title = models.CharField(max_length=150)
text = models.CharField(max_length=2000)
class Message(models.Model):
text = models.CharField(max_length=2000, blank=True, null=False)
user = models.ForeignKey(User, blank=False, null=False, on_delete=DO_NOTHING)
parent = models.ForeignKey(Thread, blank=True, null=True, on_delete=DO_NOTHING)
Django will automatically make migrations when running the command
python manage.py makemigrations
or if you only want to create migrations for the messaging
app, you can do
python manage.py makemigrations messaging
.
The output should be:
Migrations for 'messaging':
messaging\migrations\0001_initial.py
- Create model Thread
- Create model Message
If Django still does not pick up your new app and models, please make sure that your python classes correctly inherits from models.Model
as in Thread(models.Model):
, and that you define some Django model fields inside the class, e.g. text = models.CharField(max_length=2000, blank=True, null=False)
After your migrations successfully have been created, you can applythem using
python manage.py migrate
(applies all migrations)
or
python manage.py migrate messaging
(applies messaging migrations only)
- Save the related objects before the actual object being edited on django admin
- Sitemap and object with multiple urls
- Django/python How to get a list of id's from a list of Objects
- How to follow HINT: Use a callable instead, e.g., use `dict` instead of `{}`?