question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Don't know how to convert the Django class django_mysql.models.fields.json.JSONField

See original GitHub issue

I’ve just added a JSONField on my models and graphene said Don't know how to convert the Django class django_mysql.models.fields.json.JSONField. I think it wasn’t supported yet but are there any way to work around?

thanks

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

27reactions
dopeboycommented, Nov 20, 2018

Adding some more color to this in case there are others who arrive from google:

Suppose you have a django model like so:

from django.contrib.postgres.fields import JSONField

class Rapper(models.Model):
    name = models.CharField()
    bars = JSONField()

Here’s what the Query object and connecting type would look like:

from graphene.types import generic
from graphene_django.types import DjangoObjectType

from .models import Rapper

class RapperType(DjangoObjectType):
    bars = generic.GenericScalar()
    
    class Meta:
        model = Rapper

And here’s what a sample GraphQL query and response end up looking like:

{
  rapper {
    name
    bars
  }
}
{
  "data": {
    "rapper": {
      "name": "50 Cent",
      "bars": [
        "Joy wouldn't feel so good if it wasn't for pain",
        "It's your birthday"
      ]
    }
  }
}
14reactions
quytangcommented, Oct 27, 2017

I end up with this snippet, thanks

import graphene
from graphene_django.converter import convert_django_field
from django_mysql.models import JSONField

@convert_django_field.register(JSONField)
def convert_json_field_to_string(field, registry=None):
    return graphene.String()
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Django's JSONField? You probably don't need it. Here's ...
Switching from JSONB to JSON​​ If you're using Postgres with Django and importing the JSONField object, you're using JSONB. This is the default ......
Read more >
JsonField in Django for MySQL - Stack Overflow
It looks like the change is only for supporting all databases with the same unique model field. There is a similar issue in...
Read more >
How to create custom model fields - Django documentation
The trick is to tell Django how to handle saving and loading such an object. In order to use the Hand class in...
Read more >
Convert JSON data into structured data in an SQL database ...
I have a `ChatSession` model which represents all the useful information to be stored after the end of a chat session . Now,...
Read more >
Set Fields - Django-MySQL 4.8.0 documentation
On earlier versions of Django, you can use django-jsonfield-backport. Two fields that store ... This is only checked on form validation, not on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found