From an acquaintance who read the article the other day "Then, if you already have a DB in operation, can you attach Django later?" There was a selfish request, so I also verified it.
From the conclusion, it was possible. Moreover, it's easy. Obviously, it seems that the supported database products and versions are limited to some extent. This area will be helpful.
I will use the application from the previous article again, but delete the table on the database side. Now you have the Django code, but the database isn't ready yet.
Although the settings are written in settings.py, the database side is not ready. If you try to start the server in this state, it will look like this. Well that's right.
> python manage.py runserver
~Omission~
django.db.utils.OperationalError: FATAL: database "webshopping" does not exist
Prepare a suitable database and table to make it look like an existing database.
> psql -U postgres -W
Password for user postgres:********
postgres=# CREATE DATABASE newshopping;
CREATE DATABASE
postgres=# \c newshopping;
newshopping=# CREATE TABLE items(
item_name TEXT PRIMARY KEY,
price INTEGER
);
CREATE TABLE
In addition, throw in appropriate data.
postgres=# INSERT INTO items VALUES('Fragile window glass', 50000);
INSERT 0 1
postgres=# INSERT INTO items VALUES('Hard-to-break window glass', 99000);
INSERT 0 1
newshopping=# SELECT * FROM items;
item_name | price
--------------------+-------
Fragile window glass| 50000
Hard-to-break window glass| 99000
(2 lines)
So the data seems to be inserted firmly.
If you change your database product, you need to change settings.py. Please refer to this area for how to write the ENGINE part. For postgresql, the following is OK. The NAME part is the distinguished name of the database. I created it with the name newshopping earlier, so I will write it exactly as it is. There is nothing special about the other parameters. All you have to do is write the user name, password, and host name to access.
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'newshopping',
'USER': 'your_db_username',
'PASSWORD': 'your_db_password',
'HOST': 'localhost',
'PORT': '',
}
}
The URL of here posted at the beginning has detailed usage, but it is the same as the existing database. There is a tool that will make adjustments for you. How to start is like this.
> python manage.py inspectdb
class Items(models.Model):
item_name = models.TextField(primary_key=True)
price = models.IntegerField(blank=True, null=True)
class Meta:
managed = False
db_table = 'items'
If the settings.py settings are successful, the code of the class corresponding to Model will be returned as shown above. You can paste this into your Models.py or write it directly to your existing Models.py as follows:
> python manage.py inspectdb >> models.Path to py/models.py
If you set it to ">", it will be overwritten, so it is recommended to set it to ">>". After that, it is better to comment out the existing description that is no longer needed.
Looking at models.py ...
models.py
# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
# * Rearrange models' order
# * Make sure each model has one field with primary_key=True
# * Make sure each ForeignKey has `on_delete` set to the desired behavior.
# * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# Feel free to rename the models, but don't rename db_table values or field names.
from django.db import models
class Items(models.Model):
item_name = models.TextField(primary_key=True)
price = models.IntegerField(blank=True, null=True)
class Meta:
managed = False
db_table = 'items'
The class definition has been added successfully!
Well, I made a mistake in naming the table. The code was passed through the "Item" class, but the automatically generated Model class is "Items". This is because the table name in the database is "items".
But it's okay. After all, since it is a class definition, there is no problem even if you change this class name. So, I played with the class definition part a little.
models.py
# ~Abbreviation~
class Item(models.Model):
# ~Abbreviation~
Now, let's start the server and check the operation.
> python manage.py runserver
There seems to be no problem at all!
Recommended Posts