I'm having trouble trying to add south to an existing django project. Here is my situation:
My project is called alerts. Here is my folder structure:
djangoprojects/
alerts/
manage.py
alerts/
__init__.py
urls.py
views.py
models.py
wsgi.py
settings.py
common/
__init__.py
models/
__init__.py
models.py
The file alerts/alerts/models.py has the following content:
from common.models.models import *
In other words, the only thing that alerts/alerts/models.py does is import the "real" models from alerts/common/models/models.py, which has a large number of models that were created from a past project.
I also have a MYSQL database that I'll call 'existing_database'. This is the database that corresponded to the models on my past project, which ran on another machine. On that other machine I used mysqldump to get the sql file for this database and copied it over to my new machine. Then I used mysql directly to create existing_database on my new machine. So now the database existing_database should match the models in alerts/common/models/models.py.
So next I went through the documentation to try to add south to the alerts project. This is what I did:
I added 'south' and 'alerts' to the INSTALLED_APPS inside alerts/alerts/settings.py. I'm not sure why I have to add 'alerts' to settings.py inside the alerts project - you'd think the fact that the settings file was in the project folder would be enough. But it complained if I didn't add it explicitly, so I did.
I cd-ed to djangoprojects/alerts and ran ./manage.py syncdb. It said it synced some stuff, including south and alerts
Then I ran ./manage.py convert_to_south alerts. This is where I ran into problems. It said:
This application has no models; this command is for applications that already have models syncdb'd.
Make some models, and then use ./manage.py schemamigration alerts --initial instead.
So I tried running ./manage.py schemamigration alerts --initial. It created an initial migration but that migration files has almost nothing in it. All it has is a class Migration. None of my real models are anywhere to be found.
To see if it can recognize any new models, I went to common/models/models.py and added a dummy model. Then I ran ./manage.py schemamigration alerts --auto and the response I got back was "Nothing seems to have changed". So it apperas it still knows nothing about my real models.
Ao at this point I'm totally confused. Like I said, I have an existing database with real data, and I have an existing set of models code that should exactly represent that actual table structure. I just want to start using this in my alerts project and I need south because I will be adding and changing models.
How do I accomplish this?
Related
I am working on Django project that will utilize different apps to fulfill certain task. Since these apps will be referring to much same data to complete these task I figure it makes since to create a separate folder with the models like this:
--Project
--App1
--App2
--models
---model1.py
---model2.py
Right now I'm having trouble with Django recognizing the models as existing, every time I run a makemigrations Django does not detect that any changes have been made
I attempted to put a __init__.py file in the /models folder but this doesn't seem to do anything.
You should not seperate models in django projects dude! models.py file must be in app folder, That's why you can not migrate.
I have added a simple model to my reporter app in my django project
class Municipalities(models.Model):
namelsad = models.CharField(max_length=100)
geom = gismodels.MultiPolygonField(srid=4326)
when I python3 manage.py makemigrations reporter
it says no changes detected in reporter
then when I python3 manage.py migrate reporter
Operations to perform:
Apply all migrations: reporter
Running migrations:
No migrations to apply.
but there is no postgresql database table of reporter_municipalities
reporter is included in installed apps
Municipalities model is in the model.py file in reporter app
I should add I had a counties table and manually deleted it in postgresql and tried adding the municipalities model to create a table
municipalities is also in the django_content_type
but there is no table of municipalities
update
Changed Class Municipalities to Class Muni
python3 manage.py makemigrations reporter
then it asked me
Did you rename the reporter.Municipalities model to Muni? [y/N]
if I click y
then run migrate
gives me
django.db.utils.ProgrammingError: table "reporter_municipalities" does not exist
but I am so confused that table does not and never existed!!
I cannot migrate at all to the DB now because of "reporter_municipalities"
When Django looks for migrations, it searches for the app reporter as is it configured in your INSTALLED_APPS. It uses AppConfig or application name to retrieve the full package name, ie my_project.reporter. This package must be available in your PYTHONPATH. It should be available only in you development project, but could happen that it is "installed" in your virtualenv. It can happen running pip install . (without -e) or (with some configurations) running tests (I have seen this happen with tox).
In this scenario you have two my_project.reporter available to python, but you edit/update the "second" one. When you run ./manage.py makemigrations, python first find the code that you did not change (the installed one in your virtualenv), so it does not find the updates.
To check if you have this duplication, you can:
uninstall your project (pip uninstall <django-project>) to see if it refers to a "symbolic link"
If this is the case you will see something like
Uninstalling <PROJECT>:
Would remove:
/data/PROGETTI/UNICEF/odk/.venv/lib/python3.6/site-packages/<PROJECT>.egg-link
Proceed (y/n)? y
Note the egg-link at the end of the line.
OR
open a shell and type import <full_path_of_reporter> AS pkg; print(pkg.__file__) and check the filepath
Sorry I can't give a very technical answer but I got round this my creating a table with the same name directly via pgAdmin. Making and running migrations. At this point the table was recognised but obviously didn't work correctly because it had no columns. I then commented out the model in models.py and made ran migrations which removed the table. I checked on pgAdmin that the table had been removed by this point. I then deleted the migrations files (no idea if this was necessary), uncommented the model in models.py and then made and ran migrations again. Worked fine after that.
I'm late to the question but for people facing this in future, there are files inside the pycache folder inside the migrations folder with .pyc extension. Just delete those files if they are already there and run from starting, i.e., start from makemigrations again and hopefully you'll get it. I got it the same way by deleting the files in migrations folder other than init and deleting the files inside pycache folder other than init.
ty running python ./manage.py makemigrations "app_name" as the last argument, then the migrate command. this worked for me when having the same issue.
I have some problem with my database, so I deleted db.sqlite3 and Migrations manually.
than I recreate the database by using manage.py makemigrations <appname> manage. py migrate <appname>
Everything looks normal,but when I get into localhost:8000, It is a blank page without anything (even if I never change the templates).
To my confused, I can get into the admin page.
Are there any mistakes in this process?what happened to my django app?
Now what you can do is :
1) Take backup of your code
2) Create another project with other name or same name, it will have it's own sqlite db
3) Place your code in that project and then run migrations, it will recreate your db schema.
And it should work.
I was following the documentation to get python social auth on my django project
https://python-social-auth.readthedocs.org/en/latest/configuration/django.html
And after adding 'social.apps.django_app.default', to the INSTALLED_APPS in my settings.py I run this:
python manage.py makemigrations
I get this
No changes detected
Shouldn't this command be doing something. Because without this I can't migrate to create the tables that are needed for the auth.
EDIT:
I've also tried this command, and still ended up getting the same result
python manage.py makemigrations main
where 'main' is the name of my app
Today i ran into this problem. The error is in the documentation itself.
You should run $ python manage.py migrate directly. It creates tables in the database.
All the old tutorials used makemigrations, I think it was used in earlier versions of django.
My answer will be cover some basics so one can understand this types of errors easily.
Let me clear some basic terminology about migrations in the latest versions of Django(From 1.7 to under development ones).
In older versions of Django when One has to make changes in Models (Eventually in Database) then One has to use South app to apply changes in Database without affecting older database.
Django developer community has included this south app in and after Django 1.7 and also provided some simple commands to apply migrations.
When one install New app(Above question scenario) or one make changes in existing models and wish to apply changes in database then one has to tell database about what changes they want to make. To do so one has to make migrations and below is the command.
$ python manage.py makemigrations app_name
or If it is initial then no need to specify app_name, it will consider all apps.
This command will generate migrations files which will include instructions for database to make what tables and what are the attributes of that table and what will be the relationships between tables and what are changes in the current tables etc etc.
Now one has to run below command to apply this all changes in database.
$ python manage.py migrate app_name
or If it is initial then no need to specify app_name.
Please shoot any questions If you have any and please take a look at Django's official documentation on migrations for more information.
The possible reason is your project doesn't use any database models of 'social' application yet. Add a URL in your urls.py, link it to 'social' urls.
to Django < 1.8
INSTALLED_APPS ['social.apps.Django_appConfig',]
I have installed south and make some migrations. Now there's a 'migrations' directory in the folder app. My question is: when I am refactoring models, which entries in the migration directory files I must apply the changes? I think some entries are related directly with the database schema, and others with the code itself. I couldn't fina an answer to this in the south docs.
Make the changes to your models then run python manage.py schemamigration yourapp --auto. This will create the migrations for you (you'll see a new file in your migrations directory every time you do this process).
Sometimes you really need to edit a migration manually, but you should try and avoid it. Particularly if you have already run the migration (the south app keeps a record of which migrations have been run so it knows the state of your database).
South is designed to support moving between different versions of your code without breaking your database. Each migration file in the migrations directory represents a snapshot of your code (specifically a snapshot of your models.py). You migrate from version to version by running python manage.py migrate yourapp version_no