0

For some background, I worked on a Django app that had a SQLite3 database. I just created models in Django using the python manage.py startapp model_name, and just directly manipulated the database by creating API endpoints in python. I would then make API requests and change my database like that.

But this time, I've created a Django app that has a PostgreSQL database. I managed to connect my database to Django after figuring out how to install psycopg2. Then, I created a Users table in PostgreSQL through the command line. I want to get this table to show on my Django admin site, and hopefully repeat the same method for the other app. But the Django admin site can render only Django models that have been registered to the admin. So now, I'm wondering if there is a better method for working with PostgreSQL in Django.

I saw SO posts suggesting inspectdb, but this creates a large models.py file generating other classes for my app. I also couldn't successfully register the Users model to the Django admin site. I am now curious if I am working in the opposite direction. Should I be generating User models in Django and then from there, generate my PostgreSQL database?

Basically, can someone explain how to work with a PostgreSQL database in a Django app?

Jessica
  • 1,083
  • 2
  • 12
  • 27

1 Answers1

0

Can you be more specific with your question? If you are confused on how to register the User table in django admin, you can register it in admin.py. But the method of registering depends on the type of User model you are using. You can see the docs for Custom User model.

And to get the user table in your database, you need to do migrate:

python manage.py makemigrations
python manage.py migrate

Let me know if you need any help.

Srijwal R
  • 552
  • 9
  • 16
  • Basically, how do I get my PostgreSQL database (that is already attached to my Django app) to show up on the Django admin site? I tried to register it in admin.py, but that failed. I believe the difficulty I am having is because I am using PostgreSQL with Django. – Jessica Nov 27 '19 at 18:37
  • Ok, all you have to do is use inspectdb as you have already mentioned. So, you will get all the tables of your database in a models.py file. Now you can separate it out to different apps if needed. Now, register each models in respective admin.py file. And now you can migrate it. That's it. – Srijwal R Nov 28 '19 at 04:43
  • I already figured it out, I should have been more explicit (that's why I accepted your answer). Thank you anyway! – Jessica Nov 28 '19 at 04:46