Django filter JSONField list of dicts

前端 未结 3 1138
盖世英雄少女心
盖世英雄少女心 2020-12-03 21:31

I run Django 1.9 with the new JSONField and have the following Test model :

class Test(TimeStampedModel):
    actions = JSONField()

Let\'s

3条回答
  •  [愿得一人]
    2020-12-03 22:04

    You can use the django-jsonfield package, I guess it's already the one you are using.

    from jsonfield import JSONField
    class Test(TimeStampedModel):
        actions = JSONField()
    

    So to search to make a search with a specific property, you can just do this:

    def test_filter(**kwargs):
        result = Test.objects.filter(actions__contains=kwargs)
        return result
    

    If you are using PostgreSQL, maybe you can take advantage of PostgreSQL specific model fields.

    PS: If you are dealing with a lot of JSON structure you have maybe to consider using NoSQL Database.

提交回复
热议问题