如何在包含日期的JSONField上执行Django查询?

时间:2022-06-22 22:53:36

my hope was I could do something like this:

我的希望是我能做到这样的事情:

Q(data__result_started__gt = dateutil.parser.parse("5/22/18"))

or

Q(data__result_started__gt = "2018-5-22")

in the first case i get a datetime is not JSON serializable error and in the second case i get 0 results returned.

在第一种情况下,我得到一个日期时间不是JSON序列化错误,在第二种情况下我得到0结果返回。

Is there a way to do this? thanks

有没有办法做到这一点?谢谢

1 个解决方案

#1


0  

You can't use __gt postfix to filter on json sub fields. This is not supported yet in the django ORM. It is possible to do this with raw sql in postgresql, so maybe this feature will be added in a future release of Django.

您不能使用__gt postfix来过滤json子字段。 django ORM中尚不支持此功能。可以在postgresql中使用原始sql执行此操作,因此可能会在将来的Django版本中添加此功能。

If you provide your model code, someone might suggest an answer using sql.

如果您提供模型代码,有人可能会使用sql建议答案。

You can also extract the sub field using .annotate() and then use that in subsequent django QuerySet queries. See this question for an example.

您还可以使用.annotate()提取子字段,然后在后续的django QuerySet查询中使用它。请参阅此问题以获取示例。

Ordering Django querysets using a JSONField's properties

使用JSONField的属性对Django查询集进行排序

#1


0  

You can't use __gt postfix to filter on json sub fields. This is not supported yet in the django ORM. It is possible to do this with raw sql in postgresql, so maybe this feature will be added in a future release of Django.

您不能使用__gt postfix来过滤json子字段。 django ORM中尚不支持此功能。可以在postgresql中使用原始sql执行此操作,因此可能会在将来的Django版本中添加此功能。

If you provide your model code, someone might suggest an answer using sql.

如果您提供模型代码,有人可能会使用sql建议答案。

You can also extract the sub field using .annotate() and then use that in subsequent django QuerySet queries. See this question for an example.

您还可以使用.annotate()提取子字段,然后在后续的django QuerySet查询中使用它。请参阅此问题以获取示例。

Ordering Django querysets using a JSONField's properties

使用JSONField的属性对Django查询集进行排序