How do you rename apps in Django1.11 LTS? [duplicate] - python

I have a Django app named app1 with models and migrations files.
I renamed this app to app2 and I fixed all imports, urls etc...
I now have a problem with migrations files and data in tables.
How can I write migrations with the correct way to ensure:
New installation => create the new tables
Update old versions => create new tables, move data, remove old tables
Note 1: there is several tables with many Foreign Keys.
Here is my progress so far and I am not sure if I am on the good way:
I removed all older migrations
I ran python manage.py makemigrations to generate new migrations files
After these 2 steps, I can install my application but I still have problems with old version.
Question: What is the best way to migrate data?
Note 2: I don't use South.

I found a solution that's works
Fix old migrations with new Foreign Keys and new app dependencies.
Force old migrations to create tables with old app name, so for that in migrations.CreateModel.options, add db_table: 'app1_table_name'
In each migration file add replaces = [('app1', 'migration_file_name')]. This will tell to Django that current migration (app2.migration_file_name) will replace the old file, this will prevenent django to execute migrations twice.
Create a migration file to rename tables with migrations.AlterModelTable

This is eloquently answered in this blog post.
The bullet points are:
Rename the folder of the application you want to update.
Update any and import statements to the folder you updated.
Update entries for the django_content_type table to refer to the application's app_label.
Update the table names of any models you haven't explicitly set the table name of. These table names inferred by the application name and need to be updated.
Update entries for the django_migrations table and update the reference for each migration by setting the app field your new app label.
Update any namespaced folder names that are within your /static or /templates folder. For example, you might have
./foo_app/templates/foo_app/index.html and it should be updated to ./bar_app/templates/bar_app/index.html.

Renaming an app is always a tricky issue.
If you do the migration like a simple table renaming migration, at any moment the apps.get_model() for the old app cannot work because the app simply doesn't exist.
I found this answer. I know you are not using south, but I think it might work the same way, just skip the south steps.
Basically, you have to:
Dump the data, before rename, into a json file
Run the script in the answer to rename references in the json file from the app1 to app2
Rename app1 to app2 (all import references, settings.py, etc)
Run the migrations to create the tables for app2
Load the data from json file to the database
Drop the app1 tables
I hope this help.

Related

No model tables are created for a Django migration

I have searched other articles (like this one) for a solution, but nothing has helped so far...
I realized while working on my project that I needed to alter the model, changing a primary key to an AutoField rather than use a custom field. I knew this would cause a lot of issues since I have quite a few ForeignKey fields, so I wanted to drop all of my tables, clear out the migrations, and just let the migrations re-generate all of the the tables. The problem is that even though I dropped all of the tables (and eventually the whole database, due to the django_migrations table and any other latent data), cleared out all of the project/app/migrations directories, when I run manage.py makemigrations/manage.py migrate, it only creates the built-in tables (the auth_* and django_* tables) and none of my model tables get recreated.
Where else do I need to clean up in order to be able to start over with a fresh database model? I thought that clearing out the migrations directories would be sufficient, but there's something else I'm missing.
Thanks.
If you have deleted the migrations folder for an app, you have to specify the app name to create the initial migrations:
./manage.py makemigrations myapp

ProgrammingError while opening admin table

I am using django with postgres database.
When loading the admin,clicking on Course Table, I get a classic error:
ProgrammingError at /admin/user_profile/course/
relation "user_profile_course" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "user_profile_course"
I tried makemigrations, and migrate it again but no success.
Would you please help me how to handle it?
My models.py looks like:
class Course(models.Model):
name=models.CharField(max_length=30)
number_of_sessions=models.IntegerField()
student=models.ManyToManyField(User, through='Registration')
Edit:
I removed the migration folder, and make migrations again.
in the 0001initial.py the dependencies looks like:
dependencies = [
('auth', '0007_alter_validators_add_error_messages'),
]
It looks like you've removed already applied migration from your migrations folder and re-created it with different model state (with extra model Course).
That will create situation when actual structure of your database is different than state that should be after applying your migration.
Now you can either fix your migration or fix your database structure.
To accomplish first possible fix, you need to check your current state of database and compare it with your migration file. If there is something different in your migration file, simply change it to reflect your database structure.
If you don't know how to edit migration file, you can change your models to reflect database structure, remove your migration and then create it again using makemigrations.
After fixing your migration file, generate second one, just running makemigrations with having desired models structure in your models files.
For second solution, check what's inside your migration file and edit your database structure to reflect that.
And remember, do not ever delete or change already applied migration, unless you really know what you're doing!

Managing database routers in Django

I am new to Django and am trying to create a simple CRUD for an existing table in a database.
The thing is, that database is not local and I do not want Django to create its built-in tables in it -- I want it to create its tables in a local database. The correct way to do that is by using database routers, right?
I created an app myapp, and declared the external table I want to write the CRUD for in its models.py.
Now, I have written two routers (in two separate files) -- one that routes myapp to the external database (the DataRouter), and another one that routes all other requests to the local database (the SystemRouter). Is that the correct way to do that? Where should I place those files? I have tried placing them in multiple different directories inside my project, but can't make the DATABASE_ROUTERS list in settings.py find them.
For example, if I place them in the root directory for the project and make:
DATABASE_ROUTERS = ['DataRouter', 'SystemRouter']
I get:
ImportError: DataRouter doesn't look like a module path
I am really lost. Is that the best way to solve my problem? How do I route the data correcty?

How to change a sqlite table column value type from Django model?

I created a table before I code the Django app and now I merged both the app and the table with following command python manage.py inspectdb > models.py. However after some while I really need to change the value type of one of the column. Is it enough to chage it through the model file or do I need some additional steps?
If you change a field in a Django model, Django itself doesn't know how to update your database accordingly (syncdb only add tables from new models).
You have two options:
manually create your database tables;
use a migration tool like South that detects and generates migration files from changes made to your models;
I recommend the second option as it's programmatic, more error-proof and makes your life easier when you need to go back and forth between database schemas.
There is an easy way to do this. (in Django 2)
After making the necessary changes to the model.py file of your app, run command:
python manage.py makemigrations - This will generate a new file in migration folder of your app.
python manage.py migrate - This will apply those edits on actual databse.
To check if the changes have been applied, run command : .schema <tablename> in your terminal, after entering the sqlite command-line program.

How to Edit a Database Table Following a Django App Name Change

I have just created my first Django app which uses a Postgres database... which was working until I broke it by trying to rename it. I only have placeholder data in the database, and so I am not concerned about database migration, I simply want to get my app working again and with an empty database is just fine. I found this question on here: How to change the name of a Django app? which lays out the steps involved in re-naming a Django app. I have done the first two which are to 1) Rename the folder found in the project root and 2) change any references to the app in its dependencies, i.e. the app's views, the urls.py and settings.py files.
As a newbie to Django and programming, the third step is not clear to me. The instructions were to:
"Edit the database table django_content_type with the following command: UPDATE django_content_type SET app_label='' WHERE app_label='' Note: for renaming models, you'll need to change django_content_type.name"
So I have two very specific questions:
1) Where/how do I edit the database table? As in, where do I physically type this command? And/or how do I change the "django_content_type.name"?
2) I have already pushed my project to Heroku and have a working version of my app there. What will I need to do on the Heroku side to handle this change?
Thanks in advance for your patience!
You need to go to the database table manually, or use dbshell.
> ./manage.py dbshell #opens the database command line.
This places you in the command line
$> UPDATE django_content_type SET app_label='<new_label>' WHERE app_label='<old_label>'
$> <ctrl>-D
This is make you all set. You dont need to do anything else on the heroku side apart from this change.

Categories

Resources