Django JSONField dynamic contains and key lookups

2019-08-28 07:04发布

I need to filter objects by key-value pair contained in JSONField with list of dicts where key and value are model fields.

This question is related to Django JSONField list of dicts "contains" filter inside Subquery

This one works perfectly:

EntityObject.objects.filter(
    data__list_of_dicts__contains=[{'static_key': 'static_value'}]
)

But with dynamic values:

EntityObject.objects.filter(
    data__list_of_dicts__contains=[{F('field1'): F('field2')}]
)

It causes an error:

TypeError: keys must be a string

And with static key and dynamic value:

EntityObject.objects.filter(
    data__list_of_dicts__contains=[{'static_key': F('field2')}]
)

Another error:

TypeError: F(field2) is not JSON serializable

0条回答
登录 后发表回答