Import MySQL tables into Django - django

I have recently created a PHP application which connects to my MySQL database and displays the table information. Where Django is concerned, I am only familiar with them being created in SQLite by creating the relevant models.
Is there a way to do this in reverse? So have Django create models from the tables in MySQL?

You can use your existing schema to create tables. The official documentation is at: https://docs.djangoproject.com/en/1.7/howto/legacy-databases/
There are two commands:
python manage.py inspectdb
This will give you the output of running:
python manage.py inspectdb > models.py
Django won't know how you want to break these models up into separate application modules, so you'll need to do that on your own, if necessary.

Related

Pulling data from MongoDB existing databas with Django(Djongo)

So i am working on a proyect with mongodb and django(djongo) and i have to use an already existing database(MongoDB compass) and i cant find the way to pull or import the information from MongoDB, already try with: python manage.py inspectdb > models.py and python manage.py inspectdb and when create the models.py is not with the database instead it pulls the default MongoDb, already try the Documentation of django and nothing works. Anyone got some info about it?
PD: my app is totally empty and already configured the settings.py as the documentation on Django instruct.
Thanks in advance.
in your model you should specify db_table same as the collection name in your Mongo database

Import SQL file in Django (v-2.2)

I have a Django project and I am using MySQL as default. I already have created and migrated 2 models. Now I have a SQL file. I want to create new models and populate those models with this SQL file. I am using Windows 10.
I have searched this query but did not get working solution.
I think you are looking for inspectdb. Try Integrating Django with a legacy database.
First of all create a database using your SQL file.
Then add the details of your database into settings.py file.
Just run python manage.py inspectdb command, you will see all the table structures in your new database which creates with your SQL file as a Django Models structure. (convert your database into python django model).
You can get those details as a .py by running python manage.py inspectdb > models_new.py command.
Now you have all the table structures in that models.py file which create in last step.
Then change your settings.py file for old database which you migrated earlier.
Get those details from models_new.py and add into your app_name/models.py file and save it.
Run python manage.py makemigrations and python manage.py migrate.
I found useful video tutorial which is not in English, but you can get an idea about your question how to do it.

Django 1.8 - what's the difference between migrate and makemigrations?

According to the documentation here:
https://docs.djangoproject.com/en/1.8/topics/migrations/ it says:
migrate, which is responsible for applying migrations, as well as unapplying and listing their status.
and
makemigrations, which is responsible for creating new migrations based on the changes you have made to your models.
From what I understand, I first do
makemigrations
to create the migration file and then do
migrate
to actually apply the migration?
Do note though that I just began my Django project and I added my app to my "installed_apps" list. After that, I did
python manage.py runserver
and it said
You have unapplied migrations; your app may not work properly until they are applied. Run 'python manage.py migrate' to apply them.
It didn't mention anything about running makemigrations.
According the Polls tutorial:
python manage.py makemigrations <app>: Create the migrations (generate the SQL commands).
python manage.py migrate: Run the migrations (execute the SQL commands).
As Django's documentation says Migrations are Django’s way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema.
makemigrations basically generates the SQL commands for preinstalled apps (which can be viewed in installed apps in settings.py) and your newly created apps' model which you add in installed apps.It does not execute those commands in your database file. So tables doesn't created after makemigrations.
After applying makemigrations you can see those SQL commands with sqlmigrate which shows all the SQL commands which has been generated by makemigrations.
migrate executes those SQL commands in database file.So after executing migrate all the tables of your installed apps are created in your database file.
You can conform this by installing sqlite browser and opening db.sqlite3 you can see all the tables appears in the database file after executing migrate command.
As we know Django is an ORM (Object Relational Mapping). When we use the command:
python manage.py makemigrations [app_name]
It will generate the sql command to create the table corresponding to each class you made in models.py file.
then the command:
python manage.py migrate [app_name]
will create the table in database using the commands which have been generated by makemigrations.
For example, if we make a model class-
from django.db import models
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
The corresponding sql command after using makemigrations will be
CREATE TABLE myapp_person (
"id" serial NOT NULL PRIMARY KEY,
"first_name" varchar(30) NOT NULL,
"last_name" varchar(30) NOT NULL
);
and using above command, table will be created in the database when we use migrate.
You should run the command -migrate- after adding a new app under the INSTALLED APPS section in the settings.py file in order to synchronize the database state with your current set of models. Assuming you've already modified the models.py file.
When you run -makemigrations- it packages up changes to your model into individual migration files.
Normally you would first run makemigrations and then migrate.
See documentation on Django Models
It is necessary to run both the commands to complete the migration of the database tables to be in sync with your models.
makemigrations simply analyzes your current models for any changes that would be out of sync with your database and creates a migrations file that can be used to bring the in sync. If left at this point, your models would still be out of sync with your database possibly breaking your code that queries the database.
migrate is the command to "Make It So!" and apply the changes noted during the makemigrations phase.
Source
This is django's replacement for the old manual south way of making migrations, they can be used to catalog changes in your models and write out changes that will take place in the db.
Migrate is basically the old syncdb but it takes into account all the migrations made by makemigrations.
makemigrations: creates the migrations (generating SQL Command- not yet executed)
migrate: run the migrations (executes the SQL command)
But in your case, Django is asking you to migrate the DEFAULT migrations which should run before first running of server. This would have been the same warning without even creating the first app.
Make migrations : Basically it generate SQL Commands for preinstalled apps and newly created app model which you added in installed app. It dose not executed SQL commands in your database. So actual tables are not created in DB.
Migrate : Migrate execute those SQL commands which are generated by make-migration in Database file . So after migrate all the tables of installed app are created in DB.
According to the second tutorial of the django tutorial series. Migrations are:
The migrate command takes all the migrations that haven’t been applied (Django tracks which ones are applied using a special table in your database called django_migrations) and runs them against your database - essentially, synchronizing the changes you made to your models with the schema in the database.
So pretty much all it does is:
When you execute the make migrations command you're saving the 'instructions' to mysql
When you execute the migrate command, you're executing those same instructions

How to copy a database from one django project to another

I have a django project with a very large database in production. Ideally I want to copy a few tables from that environment to my dev enviroment. How can I do that without jeopardizing the integrity of the production database?
It depends on the database that you are using.
For mysql you have mysqldump
For postgre you have pg_dump
...
Take a look at Django fixtures:
https://docs.djangoproject.com/en/dev/howto/initial-data/
manage.py has two commands, "dumpdata" and "loaddata" which will dump your data to JSON so it can be imported to your dev server model by model.

How do I successfully integrate a second database with Django South?

One of our clients needs to add some geolocation data to their
site. Since they already have a database setup without GIS extensions,
I decided to create a new database (with the GIS extensions), which I
intend to use to store only the geolocation data.
I had, at some point, set things up to work alright on my development
machine (meaning, I have migrations for these new models). But now that the code has been written, I imported a DB dump
directly from the server so that my development machine exactly
mirrors the production machine, and now I can't seem to get South to
apply the migrations correctly. South seems to have several features
which allow for multiple databases, but none of them have worked so far.
What I've tried:
Just adding the model and migrating. This gives me the following
error:
AttributeError: 'DatabaseOperations' object has no attribute
'geo_db_type'
OK, so South is trying to create the model on the original database
which doesn't have the GIS extensions.
Adding the model, but specifying the 'geo' database for migrating
the 'geo' app. This gives me the following error:
django.db.utils.DatabaseError: relation "south_migrationhistory"
does not exist
I guess south expects its MigrationHistory table to exist on the 'geo'
database as well?
Allow south's models to exist on my 'geo' database.
$ python manage.py syncdb --database=geo
$ python manage.py migrate
This gives me the following error:
django.db.utils.DatabaseError: relation "<model>" already exists
I'm guessing this is because I already have MigrationHistories stored
in the other database?
South apparently has a sparsely documented feature called 'dbs'
(see:
http://south.aeracode.org/docs/databaseapi.html#accessing-the-api )
So I tried the previous three methods again replacing all instances of
"db" with "dbs['geo']".
a. Migrations run smoothly, but don't actually create any tables
in my 'geo' database.
b. Same error as when not using 'dbs' features.
c. Same error as when not using 'dbs' features.
This entire process has been extremely frustrating. Has anyone got
multiple database support up-and-running when using South?
Whenever I have modified the table models I used south and these commands to modify the structure and they always worked:
python manage.py convert_to_south "your_app"
python manage.py migrate "your_app"
I recommend running these commands after running syncdb, so your tables are created.