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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With