JSONField queries not working

Issue #25 resolved
Greg Nofi
created an issue

In your README, I see that we can do this:

  • Added better querying support: (field__contains={'key':'value','key2':'value2'} works.)

That's pretty cool. However, my teammates and I have never been able to do this. For example:

class SomeModel(models.Model):
    # <...a bunch of fields...>
    metadata = jsonfield.JSONField(null=True, blank=True,
                            verbose_name="Extra", help_text="Extra metadata")
# in the shell
>>> from models import SomeModel
>>> SomeModel.objects.get(id=1).metadata
{u'foo_number': 1,
 u'bar_number': 2}
>>> foo_number_ones = SomeModel.objects.filter(metadata__contains={'foo_number':1})
>>> foo_number_ones

In that example, I would expect there to be at least 1 result. Instead, it's an empty QuerySet.

I'm not sure why it's not working for us. Your testcases that contain this query pass for me.

Do you have any ideas?

I'm using:

  • jsonfield 0.9.2
  • Python 2.7.2
  • Django 1.4
  • MySQL 5.5.21

Comments (5)

  1. Greg Nofi reporter

    Good idea. When I run your tests with MySQL, they all pass. So that's good.

    So I took a closer look at the queries that both your tests and our project generate. Here is what the LIKE clause looks like in both:

    LIKE BINARY '%"foo": "bar"%';

    If I execute the raw SQL query for that in our project's database, it fails to find anything. But if I remove the space after the colon, it works!

    Unfortunately, I was unable to figure out anything more than that and I need to log off now. I thought I'd share in case you have any ideas?

    One thing I'm wondering about is if the dictionary is serialized into JSON differently in our 2 projects. Perhaps yours has a space and ours doesn't. (I couldn't properly check the database used in the test since it's just a test database. If I pause the test and query it in MySQL, it is empty.)


  2. Greg Nofi reporter

    I've concluded that there is no problem with this functionality in MySQL.

    I've identified the problem I was seeing with such queries in our project. One of the problems is that I also had the jsonfield package installed and that was confusing me. :(

    Sorry for the false alarm.

  3. Log in to comment