Search code examples
pythonjsondjangodjango-2.0inspectdb

inspectdb command for json in django2 generates django.contrib.postgresql.fields.JSONField instead of django.contrib.postgres.fields.JSONField?


I switched to django 2 because it supports detecting json fields with inspectdb, but when I run the inspectdb it generates django.contrib.postgresql.fields.JSONField which I don't know where to import it from.

As specified in django documentation here django_inpectdb_doc

I understand that can fix it by adding import django.contrib.postgres.fields.JSONField bu the problem is that it was automatically generated django.contrib.postgresql.fields.JSONField (notice the bold text).

class AsyncResultsStore(models.Model):
    task_id = models.CharField(max_length=255)
    created = models.DateTimeField(blank=True, null=True)
    status = models.CharField(max_length=255)
    result = django.contrib.postgresql.fields.JSONField(blank=True, null=True)
    info = models.CharField(max_length=255)
    arguments = django.contrib.postgresql.fields.JSONField(blank=True, null=True)
    chip_meas_result = models.ForeignKey(Chipmeasurementresult, models.DO_NOTHING, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'async_results_store'

I want to do:

import django.contrib.postgres.fields

but that is not possible because the generated line is:

django.contrib.postgresql.fields

postgressql instead of postgres

In the link of documentation above it is specified that I should put in settings.py installed apps: 'django.contrib.postgres'

which I did.

should I import from somewhere else or is this a buggy behavior in django2?

If I do import django.contrib the error that I get is:

result = django.contrib.postgresql.fields.JSONField(blank=True, null=True)
AttributeError: module 'django.contrib' has no attribute 'postgresql'

Solution

  • This was a bug in Django's introspection code. You can change the models to use django.contrib.postgres.fields, and add import django.contrib.postgres.fields to your models.py.

    The issue will be fixed in Django 2.0.5 by Django ticket 29307.