Django 1.8, makemigrations not detecting newly added app - python

I have an existing django project with a,b,c apps. All of them are included in installed apps in settings file. They have their own models with for which the migrations have already been ran. Now, if I add a new app d, add a model to it, include it in installed apps and try to run a blanket makemigrations using python manage.py makemigrations I get a no changes detected message. Shouldn't the behaviour be like it detects a new app and run an initial migration for that? I know I can do it manually using python manage.py makemigrations d but I want to do it with using python manage.py makemigrations command. Can someone provide explanation for this behavior?

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

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)

Related

Python Django error "no module named ..." after manage.py runserver. Did I organize my files correctly?

I'm new to Python and Django. I'm trying to create a user authentication system with Djoser following an online tutorial. I am getting this error "no module named "auth_system"" when I try to run the server to test the connection with Postman.
Is it because of the way that I organized the folders that my "auth_system" file is not recognized?
Also, is the way that I put all the files into the virtual environment folder correct?
Have you added auth_system to installed app in settings file:
INSTALLED_APPS = [
'auth_system',
]
I will give you a quick reference for how to start a django project and review the arranged files.
Be sure that you have properly installed django.
$ python -m django --version
2)Move to the folder where you will create your project and command:
$ django-admin startproject mysite
Where mysite is the name of your project.
The created files of the project mysite will look like the following:
mysite/
manage.py
mysite/
init.py
settings.py
urls.py
asgi.py
wsgi.py
Now you can test if everything is ok
$ python manage.py runserver
July 08, 2021 - 10:28:12 Django version 3.2, using settings
'mysite.settings' Starting development server at
http://127.0.0.1:8000/ Quit the server with CONTROL-C.
4)You can add a project with the next instruction:
$ python manage.py startapp polls
The created directory polls, looks like this:
polls/
init.py
admin.py
apps.py
migrations/
init.py
models.py
tests.py
views.py
Then you can create a superuser like this:
$ python manage.py createsuperuser --username=admin --email=admin#example.com
Or normal users like this (in python prompt):
from django.contrib.auth.models import User
user = User.objects.create_user('user01', 'user01#example.com', 'user01password')
Hope this useful for you!
More information:
https://docs.djangoproject.com/en/3.2/intro/tutorial01/

Error in Django migrations, no changes detected

I've correctly done the "initial setup" with python manage.py migrate command and now in my mongodb database I see these collections:
__schema__
auth_group
auth_group_permissions
auth_permission
auth_user
auth_user_groups
auth_user_user_permissions
django_admin_log
django_content_type
django_migrations
django_session
with object inside them so I'm pretty sure that I did it correctly and if I do it now it says:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
I think this is all normal, then i created this models.py file
models.py
from django.db import models
# Create your models here.
class Customer(models.Model):
name = models.CharField(max_length=200, null=True)
surname = models.CharField(max_length=200, null=True)
phone = models.CharField(max_length=200, null=True)
email = models.CharField(max_length=200, null=True)
date_created = models.DateTimeField(auto_now_add=True, null=True)
Here is part of my settings.py file:
INSTALLED_APPS = [
'mysite',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Folder Structure:
mysite
mysite
__init__.py
settings.py
other files
polls
migrations
other files
__init__.py
When I try to do python manage.py makemigrations I get this "No changes detected". Adding my app name doesn't solve the problem. I have a migrations folder with init.py (with the __) in it. I don't understand why it worked for the initial setup and now it doesn't. If i put some syntax error in the models.py file the messages I get after running the commands are the same, so maybe models.py is being searched in another folder? Really don't know, anyway hope i wrote everything necessary, I will reply as fast as I can if you need more informations!
Here is a few things to check to make sure your migrations will go through.
Make sure you created the app using django-admin startapp mysite.
Make sure you've saved the models file after adding the model into the mysite/models.py.
Add the app name to the installed app in the settings.py, but make sure to add it after all apps.
In your case I see that you've added mysite in the installed app when in reality you should add the app name not the project name, which in your case the polls app.

operational error: no such table django Every Time I create models

I am using Django 2.0, Python
Every Time I am creating new Model when I do
python3 manage.py makemigrations
it shows no changes detected and on migrate no migrations to apply
Even I tried
python3 manage.py makemigrations home
shows
Migrations for 'home':
home/migrations/0001_initial.py
- Create model FAQ
- Create model Topic
and then on
python3 manage.py migrate home
It still shows
Operations to perform:
Apply all migrations: home
Running migrations:
No migrations to apply.
and when I try to open that table in admin.
it shows
OperationalError at /admin/home/faq/
no such table: home_faq
however it'd worked when I'd deleted migrations and database and re migrated everything.
But I can't delete everything Every Time.
my models.py
from django.db import models
# Create your models here.
class Topic(models.Model):
topic_title = models.CharField(max_length=200,unique=True)
topic_discription = models.TextField()
no_of_posts = models.IntegerField()
def __str__(self):
return self.topic_title
class FAQ(models.Model):
question = models.CharField(max_length=800,unique=True)
answer = models.TextField()
timestamp = models.DateTimeField(auto_now_add=True,auto_now=False)
author = models.CharField(max_length=100)
author_info = models.TextField()
def __str__(self):
return self.question
I've registered it on admin.py
from django.contrib import admin
from .models import Topic,FAQ
# Register your models here.
admin.site.register(Topic)
admin.site.register(FAQ)
EDIT #1:
Okay now I cleared all migrations and db.sqlite3 and ran
# python3 manage.py makemigrations
No changes detected
# python3 manage.py makemigrations home
Migrations for 'home':
home/migrations/0001_initial.py
- Create model FAQ
- Create model Topic
# python3 manage.py migrate home
Operations to perform:
Apply all migrations: home
Running migrations:
Applying home.0001_initial... OK
It does work but I rebuild the database for that.
Because It's not in production yet so its okay but It may become big problem later.
thing to notice is on
python3 manage.py makemigrations
It shows
No changes detected
but created migrations when
python3 manage.py makemigrations home
and have friendly response on
python3 manage.py migrate home
So even if there is any way to force rebuild the model in db then could do.( python3 manage.py force migrate )
^ Could be possible solution. if there exist force
EDIT #2:
Installed Apps:
INSTALLED_APPS = [
'home.apps.HomeConfig',
'UserProfile.apps.UserprofileConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

makemigrations not able to find app within INSTALLED_APPS

I am trying to port an existing django 1.4 project to django 1.7
Here is my tree structure before I ported the project to django 1.7
Project
- MainApp
- manage.py
- settings.py
- another_sub_app
- another_sub_app2
While porting the project I had to move manage.py and sub_apps one level up.
Project
- another_sub_app
- another_sub_app2
- manage.py
- MainApp
- settings.py
I used "South" for database migrations and had to use "python manage.py schemamigration " to make migrations. Now (after porting) I will be using "python manage.py makemigrations " to migrate app specific model changes.
However, while running "python manage.py makemigrations " I am getting:
App 'app_name' could not be found. Is it in INSTALLED_APPS?
I have the app under INSTALLED_APPS and due to the structural changes I made I also tried including . inside INSTALLED_APPS. But this shows the same error again.
My question is has anyone tried porting a project to django 1.7 and has had similar issue?
I haven't tried porting a project to django 1.7. However, I use django 1.7 and your settings.py should look like this
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'your_app_name', #In your case, MainApp
'your_app_name', #In your case, another_sub_app
'your_app_name', #In your case, another_sub_app2
)
And the tree structure is
Project
- Project
- settings.py
- MainApp
- another_sub_app
- another_sub_app2
- manage.py

"python manage.py syncdb" not creating tables

I first ran
python manage.py syncdb
and it created the database and tables for me, then I tried to add more apps, and here's what I did:
create apps by
python manage.py startapp newapp
Then I added 'newapp' to INSTALLED_APPS in setting.py:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'newapp',
)
At last I ran syncdb:
python manage.py syncdb
and here's the result I get:
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
I checked my db and there is no table named newapp, no table's name including newapp.
I also ran into this issue, and was able to get around it by removing the migrations folder inside my app. Somehow that had already gotten created and was tricking syncdb into thinking that it was fully migrated already, but those migration scripts didn't actually do anything useful. Obviously don't try this if you actually have migrations you want to save, but I was working with a brand new app and models.
If you run:
python manage.py inspectdb > somefile.txt
You can get quickly check out if your database structure is matching your django models.
I tried most of the ideas above:
making sure the models.py is imported (verified that the module executed during a makemigrate),
deleting the migrations folder
setting managed = True (this is default anyways),
used the python manage.py inspectdb (which correctly dumped the table, if I had created it manually).
The key was simply to run a makemigrations on the app separately:
python manage.py makemigrations <app_name>
as part of performing the makemigrations step. Then you do
python manage.py migrate
afterwards as usual.
(applies to Django 1.10, using Postgres 9.5).
Django documentation
Credit, related post
i got same problem, but i didn't have any "migration" folder. I solved it like below.
I just added app_label with the model code,
class MyModel(models.Model):
...
class Meta:
managed = True # add this
app_label = 'myapp' # & this
Also, make sure it is discoverable by referencing it in myapp/models/__init__.py
from model_file.py import MyModel
You should be using this command
python manage.py migrate
where you were runing syncdb from the location where manage.py resides
I was having the same problem and noticed that it had created a db.sqlite3 file that didn't seem empty:
$ ls -l
-rw-r--r-- 1 sauron staff 122880 Jul 31 01:22 db.sqlite3
I tried running sqlite again using the filename as an argument and that worked:
$ sqlite3 db.sqlite3
SQLite version 3.7.13 2012-07-17 17:46:21
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .schema
CREATE TABLE "auth_group" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(80) NOT NULL UNIQUE
);
... many rows ...

Categories

Resources