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

Hi

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
https://docs.djangoproject.com/en/3.1/howto/legacy-databases/

https://docs.djangoproject.com/en/3.1/ref/django-admin/#postgresql

https://docs.djangoproject.com/en/3.1/ref/contrib/postgres/#module-django.contrib.postgres

as my understanding, the article recommends me to install ‘django.contrib.postgres’ in INSTALLED_APPS in settings.py so I suppose that it is a build-in package from Django but we must manually install in settings.py
and then run python manage.py 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.
https://pypi.org/project/django-jsonfield-backport/
https://pypi.org/project/django-postgres-extensions/
https://pypi.org/project/django-contrib-postgres/

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

Thank you
Trong

  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.

Ken

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.

Hi,
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 **
3.run 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.

Ken

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.