Django orm "annotate" not working with "only" - django

I want to select only one column with "only" and rename it.
The code I want in SQLServer is as follows:
SELECT [table].[col1] AS [new_col1] FROM [table]
in django orm:
model.objects.annotate(new_col1=F('col1').only('col1')).all()
When i change it to sql query it is like this:
SELECT [table].[col1], [table].[col1] AS [new_col1] FROM [table]
and below orm code not working:
model.objects.annotate(new_col1=F('col1').only('new_col1')).all()
I don't want to use "values" or "values_list".
Please help me how I can do it.

Related

Code (using Patch function) to save update from PowerApps to SQL table not working

this is my code in PowerApps:
OnSelect = Patch(ProjectTrackerPowerApps, {ProjectTrackerID: First([#PowerBIIntegration].Data).ProjectTrackerID, 'Meeting Notes':TextInput1.Text } )
where ProjectTrackerPowerApps is the SQL table name, and "Meeting Notes" is the field in PowerApps that is being edited and the edits are to be read back to the SQL table using the OnSelect Code.
But it's not working, i.e. I don't see the changes in the SQL table.
I also tried this other code - slightly different than the 1st but it also didn't work:
OnSelect = Patch(ProjectTrackerPowerApps, defaults(ProjectTrackerPowerApps), {Field:Screen1.Selected.ProjectTrackerID})
Can you please review the code and help me correct so it works?
Thanks!

Is there any way to get the displayed text in the "Select List" without using SQL? (Oracle APEX 21.1)

Anyone help me?
"Select List" (name: P2_SHOPS_LIST) that is created with the following SQL
SQL Statement: SELECT SHOP_NAME, GROUP_ID FROM T_ENTRY_SHOPS WHERE ID=:P2_LOV_ID;
It is necessary "GROUP_ID" because it is PK . But I need edit "SHOP_NAME" value and display to Text Field.
I think I can get the currently displayed SHOP_NAME by combining the above SQL with the selection row number, but is there any way to access this value with No SQL? Like
:P2_SHOPS_LIST.SHOP_NAME
(This gave me an error XD).
In the context of JavaScript, you can use the following
$('#P2_SHOPS_LIST option:selected').text()
This can be passed through to PL/SQL via a dynamic action, such as on change of your select list.
Or you could set your text item on change via a JS based action, using 'Set Value' and that code as the expression.

Is there any way to convert this into django orm?

I needed to get all the rows in the table1 even if it is not existing in table2 and display it as zero. I got it using raw sql query but in django ORM i am getting the values existing only in table2. The only difference on my django orm is that iI am using inner join while in the raw sql query I am using left join. Is there any way to achieve this or should I use raw sql query? Thanks.
Django ORM:
total=ApplicantInfo.objects.select_related('source_type').values('source_type__source_type').annotate(total_count=Count('source_type'))
OUTPUT OF DJANGO ORM IN RAW SQL:
SELECT "applicant_sourcetype"."source_type", COUNT("applicant_applicantinfo"."source_type_id") AS "total_count" FROM "applicant_applicantinfo" INNER JOIN "applicant_sourcetype" ON ("applicant_applicantinfo"."source_type_id" = "applicant_sourcetype"."id") GROUP BY "applicant_sourcetype"."source_type"
RAW SQL:
SELECT source.source_type, count(info.source_type_id) as total_counts from applicant_sourcetype as source LEFT JOIN applicant_applicantinfo as info ON source.id = info.source_type_id GROUP BY source.id
You cannot query on ApplicantInfo if you want a left join with it. Query on SourceType instead:
qs = (SourceType.objects
.values('source_type')
.annotate(cnt=Count('applicantinfo'))
.values('source_type', 'cnt')
)
Since you haven't posted the models, you may need to adapt the field names to make it work.

Adding calculated column to query result in Korma

How do I do the equivalent of
SELECT *, id=1 AS calc_column FROM table
in Korma? (Obviously, id=1 is just a simple example).
Korma has sqlfn which can be used in fields as show here. If even that doesn't fit your need then you always have the raw function to feed in raw sql in main query.

postgresql full text search query to django ORM

I was following the documentation on FullTextSearch in postgresql. I've created a tsvector column and added the information i needed, and finally i've created an index.
Now, to do the search i have to execute a query like this
SELECT *, ts_rank_cd(textsearchable_index_col, query) AS rank
FROM client, plainto_tsquery('famille age') query
WHERE textsearchable_index_col ## query
ORDER BY rank DESC LIMIT 10;
I want to be able to execute this with Django's ORM so i could get the objects. (A little question here: do i need to add the tsvector column to my model?)
My guess is that i should use extra() to change the "where" and "tables" in the queryset
Maybe if i change the query to this, it would be easier:
SELECT * FROM client
WHERE plainto_tsquery('famille age') ## textsearchable_index_col
ORDER BY ts_rank_cd(textsearchable_index_col, plainto_tsquery(text_search)) DESC LIMIT 10
so id' have to do something like:
Client.objects.???.extra(where=[???])
Thxs for your help :)
Another thing, i'm using Django 1.1
Caveat: I'm writing this on a wobbly train, with a headcold, but this should do the trick:
where_statement = """plainto_tsquery('%s') ## textsearchable_index_col
ORDER BY ts_rank_cd(textsearchable_index_col,
plainto_tsquery(%s))
DESC LIMIT 10"""
qs = Client.objects.extra(where=[where_statement],
params=['famille age', 'famille age'])
If you were on Django 1.2 you could just call:
Client.objects.raw("""
SELECT *, ts_rank_cd(textsearchable_index_col, query) AS rank
FROM client, plainto_tsquery('famille age') query
WHERE textsearchable_index_col ## query
ORDER BY rank DESC LIMIT 10;""")