#29147: Postgres JSONField missing to_python
-------------------------------------+-------------------------------------
     Reporter:  Javier Buzzi         |                    Owner:  Williams
                                     |  Mendez
         Type:  Bug                  |                   Status:  closed
    Component:  contrib.postgres     |                  Version:  1.9
     Severity:  Normal               |               Resolution:  invalid
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  1                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Javier Buzzi):

 I added this (copy and paste from hstore.py)

 {{{
 --- a/django/contrib/postgres/fields/jsonb.py
 +++ b/django/contrib/postgres/fields/jsonb.py
 @@ -41,6 +41,14 @@ class JSONField(CheckFieldDefaultMixin, Field):
          self.encoder = encoder
          super().__init__(verbose_name, name, **kwargs)

 +    def to_python(self, value):
 +        if isinstance(value, str):
 +            value = json.loads(value)
 +        return value
 +
 +    def value_to_string(self, obj):
 +        return json.dumps(self.value_from_object(obj))
 +
      def db_type(self, connection):
          return 'jsonb'
 }}}
 ran the tests:


 {{{
 $ /django/tests/runtests.py postgres_tests.test_json --failfast
 --settings=mysettings
 Testing against Django installed in '/usr/local/lib/python3.6/site-
 packages/Django-2.1.dev20180313024248-py3.6.egg/django' with up to 4
 processes
 Creating test database for alias 'default'...
 Cloning test database for alias 'default'...
 Cloning test database for alias 'default'...
 Cloning test database for alias 'default'...
 Cloning test database for alias 'default'...
 System check identified no issues (0 silenced).
 .......................................................
 ----------------------------------------------------------------------
 Ran 55 tests in 0.118s

 OK
 Destroying test database for alias 'default'...
 Destroying test database for alias 'default'...
 Destroying test database for alias 'default'...
 Destroying test database for alias 'default'...
 Destroying test database for alias 'default'...

 }}}

 Here are all the steps on how i got it to work:

 {{{
 $ docker run --rm -it python:3.6.4 bash
 apt-get update
 git clone https://github.com/django/django.git
 cd django
 sed -i "s/^exit 101$/exit 0/" /usr/sbin/policy-rc.d
 apt-get install -y libmemcached-dev postgresql vim postgresql-contrib
 python setup.py install
 pip install -rtests/requirements/py3.txt -rtests/requirements/postgres.txt
 echo "SECRET_KEY = 'hellooooo'
 DATABASES = {
     'default': {
         'ENGINE': 'django.db.backends.postgresql_psycopg2',
         'NAME': 'django',
         'USER': 'django',
         'PASSWORD': 'django',
         'HOST': 'localhost',
         'PORT': 5432,
     }
 }" > tests/mysettings.py
 echo "local   all         all                         trust" >>
 /etc/postgresql/9.4/main/pg_hba.conf
 echo "diff --git a/django/contrib/postgres/fields/jsonb.py
 b/django/contrib/postgres/fields/jsonb.py
 index 3c27607..49b65ef 100644
 --- a/django/contrib/postgres/fields/jsonb.py
 +++ b/django/contrib/postgres/fields/jsonb.py
 @@ -41,6 +41,14 @@ class JSONField(CheckFieldDefaultMixin, Field):
          self.encoder = encoder
          super().__init__(verbose_name, name, **kwargs)

 +    def to_python(self, value):
 +        if isinstance(value, str):
 +            value = json.loads(value)
 +        return value
 +
 +    def value_to_string(self, obj):
 +        return json.dumps(self.value_from_object(obj))
 +
      def db_type(self, connection):
          return 'jsonb'

 " > mychanges.diff
 patch django/contrib/postgres/fields/jsonb.py mychanges.diff
 service postgresql start
 echo "set the password to 'django'"
 su postgres -c 'createuser --superuser django -P'
 /django/tests/runtests.py postgres_tests.test_json --failfast
 --settings=mysettings

 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29147#comment:7>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/069.772d275875e7aa6a32ef32dfbba4acf2%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to