How to enable json/jsonb type from django.contrib.postgres installation


I use the existing Postgres database for my project.
Some tables in my database need to use JSON / jsonb field.

I read how to inspect existing database along with adding an extra feature especially data type for Postgres as 3 links below

as my understanding, the article recommends me to install ‘django.contrib.postgres’ in INSTALLED_APPS in so I suppose that it is a build-in package from Django but we must manually install in
and then run python inspectdb

once I ran the command to inspect the existing database, it works well but the column “additional_info” that it is JSON field in the database still is models.TextField ( This field type is a guess) as the figure below.

1. how to fix this problem? OR ELSE do I have to install any addtional package ?
As I find out, there are a lot as below for extra feature in Posgres.

2. How to select only some tables in the existing database as desired to inspect in order to create (not all tables in database)

Thank you

  1. InspectDb is not a 100% solution. It’s known and documented that you are going to need to manually adjust the models it creates. There is no problem to fix. For those fields that are misidentified, just edit the generated model to make it the correct field type.

  2. See the referenced documentation from above for identifying which tables to examine.


This might be a regression in Django 3.1. Previously it seems that inspectdb could detect jsonb columns as JSONFields — see ticket #30476, which tried to add the same for “json” (no b) columns. If you try on Django 3.0, do you get JSONFields? In which case we might want to add this back on 3.1, for all DB’s.

as I suggest above.

I summarize as my understanding like below.
1.i can put django.contrib.postgres in INSTALLED_APPS without any additoinal pip installation. Right?

**2. after runing inspectdb then I can manually adjust the models like json datatype instead of text ** makemigrations and migrate to update schema to database

  1. Yes. All the django.contrib packages are part of the Django installation. However, you do also need the psycopg2 package. (I typically use psycopg2-binary.)
  2. Yes. And, there may be other changes you may want or need to make as well.
  3. Yes. Keep in mind that inspectdb marks all models as manage=False, which means that makemigrations / migrate aren’t going to affect those tables.


I already installed psycopg2 , is it ok?

Sure is - your choice either way. I was just mentioning which one I use. There’s no “right” or “wrong” answer in this case.

well done.
I really appreciate your support.