How can I write this sql query in django orm? - python

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 ?
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:
YourModel.objects.raw('select * from your table'): #---> Change the model name and query

Here you have PostgreSQL system functions. Django ORM doesn't have all these functions. Django ORM can cover it partially.
Now you can do one of these.
Write ''raw sql'' and execute it.
You can write python #staticmethods to implement that functionality then use it in your view.py. (Here you'll have python-power)

Related

Using SQLAlchemy without Predefined Tables for SQL Generation

I want to write my SQL queries in Python so that I can make them parameterizable and composable.
I was hoping to do something like the following via SQLAlchemy, but the expression API doesn't seem to support it without automapping my database.
select('*').from_('table')
How can I accomplish that with SQLAlchemy or should I look to use a different tool?

Django - Using a complex query

I've read almost all of the related questions and still can't figure out how to execute the following query in Django.
Using the Django standard tables for Auth I've added a group called 'approvers'. I need to query to return all approvers. In SQLite designer I developed the following sql:
select auth_user.email, auth_user.first_name, auth_user.last_name
from
auth_user, auth_user_groups
where
auth_user.id = auth_user_groups.user_id
and
auth_user_groups.group_id in
( select auth_group.id from auth_group where auth_group.name = "approvers")
It seems that I should be able to do this by using the raw method on the models, but would like to understand how to use the Django ORM to access this if it's a better more acceptable approach.
You can .filter(…) [Django-doc] with:
User.objects.filter(groups__name='approvers')

how to write simple select query with where clause in django object view.py?

I am very new to Django and want to ask one doubt that how to write simple select query with where clause in django view.py?
e.g. I have a sqlite db with a column name "result" and I want to fetch the result in view.py by object.filter or similar thing like that.
SQL Query-
select result from polls_testreults where css_class="event-info" and requestId="201456"
What I am trying to achieve-
reslink= TestReults.objects.values('result',flat=True).filter(requestId=checkbox)
I am not getting any output in reslink and I think query is also wrong.
kindly help me out
test_results = TestResult.objects.filter(css_class="event-info", requestId="201456")
You need to go through the basic django documentation first, I believe you would figure out a lot more than this once you get started.

Postgresql Prefix in django orm

How could I go about utilizing the Postgres Prefix Plugin within django? Is there a way to just append an additional WHERE clause to the queries that django runs without going into raw SQL?
Maybe something like this?
Model.objects.filter(field1=2, field2__in=[1,2,3]).where("prefix #> '0123456789'")
Yes it is possible, this is the extra method on QuerySet. Something like this should do the trick:
Model.objects.filter(...).extra(where=["prefix #> '0123456789'"])

Werkzeug without ORM

How do I use the Werkzeug framework without any ORM like SQLAlchemy? In my case, it's a lot of effort to rewrite all the tables and columns in SQLAlchemy from existing tables & data.
How do I query the database and make an object from the database output?
In my case now, I use Oracle with cx_Oracle. If you have a solution for MySQL, too, please mention it.
Thanks.
SQLAlchemy supports reflection so you don't have to do that. Take a look at the autoload parameter of Table, you can even make this work with the ORM.
Is it a problem to use normal DB API, issue regular SQL queries, etc? cx_Oracle even has connection pooling biolt in to help you manage connections.
maybe this is what i looking for http://www.sqlalchemy.org/trac/wiki/SqlSoup
and ht*p://spyced.blogspot.com/2006/04/introducing-sqlsoup.html
so i don't have to declare the table to get the object
rp = db.bind.execute('select * from mupp')
a = rp.fetchall()
a[0].name
that's great...thanks for all inspiring response

Categories

Resources