Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

264
Vistas
Django MySQL Query Json field

I have a MySQL database with a table containing a JSON field called things. The JSON looks like this

things = {"value1": "phil", "value2": "jill"}

I have collection of objects that I have pulled from the database via

my_things = Name_table.objects.values

Now, I'd like to filter the my_things collection by one of the JSON fields. I've tried this

my_things = my_things.filter(things__contains={'value': 'phil'})

which returned an empty collection. I've also tried

my_things = my_things.filter(things={'value': 'phil'})

and

my_things = my_things.filter(things__exact={'value': 'phil'})

I'n using Django 1.10 and MySQL 5.7 Thoughts?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

It depends on how exactly do you store JSON in field. If you use django-jsonfield, then your things will be string without spaces, with strings inside of quotation marks: '{"value1":"phil","value2":"jill"}'.

Then, via docs:

my_things = my_things.filter(things__contains='"value1":"phil"')

should return your filtered QuerySet, because

>>> tmp_str = '{"value1":"phil","value2":"jill"}'
>>> '"value1":"phil"' in tmp_str
True
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda