Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a table in django app models from PostgreSQL?

I have created a django project named Book Store. In my project I have one app named books.

I am using PostgreSQL DBMS. I have a database called books. In that database, I have a table named novel. How do I add this table in the models.py file inside books app?

I want to register this table under admin site. After successfully adding this table in the models.py file, I believe, I shall be able to register the table in the admin site by writing the following codes in the admin.py file under books app.

from django.contrib import admin
from . models import novel

admin.site.register(novel) 

I also want to know that if my approach is correct or is there any better way available? As I am new to django, I don't know much about it.

I have read django-book but didn't find out my answer. There is an article about SQLite database though.

like image 959
Rubel Hosen Avatar asked Feb 17 '18 00:02

Rubel Hosen


1 Answers

The convention here is that you should create your models first then you can create database tables to bind with by migrations:

python manage.py makemigrations
python manage.py migrate

there is another way which you can make django automatically create models for you based on the already implemented database tables:

python manage.py inspectdb > models.py

https://docs.djangoproject.com/en/2.0/howto/legacy-databases/

like image 114
Peter Sobhi Avatar answered Nov 15 '22 03:11

Peter Sobhi