5

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.

1 Answer 1

11

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/

Sign up to request clarification or add additional context in comments.

2 Comments

Second one works but imports all the table in the app. First one doesn't work. I have create model as class novel(models.Model): and then run the first two command in shell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.