1

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'
Erindy
  • 155
  • 11
  • I've created a Django ticket about this. If you let me know your GitHub username I can credit you in the commit message, or I can just say 'Erindy on Stack Overflow' if you prefer. – Alasdair Apr 09 '18 at 16:32
  • @alasdair here is my github https://github.com/erindy I spent quite sometime thinking it was due to improper django 2 upgrade. – Erindy Apr 09 '18 at 16:55
  • The good news is that thanks to your question it will be fixed in the next Django release 2.0.5. – Alasdair Apr 09 '18 at 17:05
  • @alasdair thanks and happy to contribute, please considering upvoting the question if you found it useful. – Erindy Apr 10 '18 at 08:38

1 Answers1

0

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.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • The idea is not have to intervene manually on the models.py file, so I can run scripts automatically with some cron job and keep all the django projects in sync with the database. I am thinking of using a custom manage.py command to add these imports. – Erindy Apr 09 '18 at 17:16
  • [The docs](https://docs.djangoproject.com/en/2.0/howto/legacy-databases/#auto-generate-the-models) say that `inspectdb` is meant as a shortcut, not as definitive model generation. Even after the bug has been fixed, you'll still have to add the `import django.contrib.postgresql.fields` to the models. – Alasdair Apr 10 '18 at 14:04
  • I understand that, and it is exactly what I did, but my aim was to integrate inspectdb into some deployment script so that I can make deployments without meddling with the models manually. To achieve that I added a simple external python script which adds "from django.contrib.postgres.fields import JSONField". At this point I am wondering whether it is a good practice or not as I don't see any issue with it. Also I agree and understand that this is shortcut, but wouldn't there be more power in django if this became a full feature. – Erindy Apr 10 '18 at 14:11
  • Adding the imports would probably be worth while, as long as the code changes were not too complicated. It doesn't appear to be a very commonly used part of Django however - it took several months [after the bug was introduced](https://github.com/django/django/commit/0cbb6ac00770d201b8f30a404d516b97fe41485d#diff-08060e6f1a44dec22fbf183d91ad8be2) until you came across it. – Alasdair Apr 10 '18 at 14:18
  • Coming from another web framework where a similar feature was used quite extensively I think it would be useful as well in django as it would allow to just plug the legacy database easily into a django application. inspectdb already provides that only the reliability needs to be increased as it would open way to interesting opportunities in microservices and other integrations. Unless there are other tools that achieve what inpectdb does I am not aware of. – Erindy Apr 10 '18 at 14:42