简体   繁体   English

如何在 django orm 中编写此 sql 查询?

[英]How can I write this sql query in django orm?

I have a sql query that works like this, but I couldn't figure out how to write this query in django. Can you help me?我有一个像这样工作的 sql 查询,但我不知道如何在 django 中编写这个查询。你能帮帮我吗?

select datetime,
       array_to_json(array_agg(json_build_object(parameter, raw)))  as parameters
  from dbp_istasyondata
 group by 1
 order by 1;

You can use raw function of django orm. You can write your query like this:您可以使用raw的 function of django orm。您可以这样编写查询:

YourModel.objects.raw('select * from your table'): #---> Change the model name and query

Here you have PostgreSQL system functions.这里有 PostgreSQL 个系统函数。 Django ORM doesn't have all these functions. Django ORM 没有所有这些功能。 Django ORM can cover it partially. Django ORM 可以部分覆盖。

Now you can do one of these.现在你可以做其中之一。

  1. Write ''raw sql'' and execute it.编写“原始 sql”并执行它。
  2. You can write python @staticmethods to implement that functionality then use it in your view.py.您可以编写 python @staticmethods 来实现该功能,然后在您的 view.py 中使用它。 (Here you'll have python-power) (在这里你将拥有 python-power)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM