OperationalError: sub-select returns 2 columns - expected 1 in Django QuerySetHow to combine 2 or more querysets in a Django view?How do I do a not equal in Django queryset filtering?django - convert a list back to a querysetGetting the SQL from a Django QuerySetDjango queryset count with extra selectDjango queryset .count() .filter() not working on custom model managerDjango, How to make multiple annotate in a single querysetDjango filter a reverse relationship of each item in a querysetAnnotate not returning the expected resultsDjango queryset for multilevel Foreignkey reference
What is /etc/mtab in Linux?
Magical attacks and overcoming damage resistance
Zonal Statistics is returning null values in ArcGIS
What is the unit of time_lock_delta in LND?
Does a large simulator bay have standard public address announcements?
Is Electric Central Heating worth it if using Solar Panels?
How can I get rid of an unhelpful parallel branch when unpivoting a single row?
"My boss was furious with me and I have been fired" vs. "My boss was furious with me and I was fired"
How did Captain America manage to do this?
"Whatever a Russian does, they end up making the Kalashnikov gun"? Are there any similar proverbs in English?
I preordered a game on my Xbox while on the home screen of my friend's account. Which of us owns the game?
Why did C use the -> operator instead of reusing the . operator?
Combinatorics problem, right solution?
Partitioning values in a sequence
Can a level 2 Warlock take one level in rogue, then continue advancing as a warlock?
Where was the County of Thurn und Taxis located?
Why do games have consumables?
My admission is revoked after accepting the admission offer
Don’t seats that recline flat defeat the purpose of having seatbelts?
How much cash can I safely carry into the USA and avoid civil forfeiture?
Can a barbarian keep raging if she shoves an enemy on her turn?
Find the identical rows in a matrix
Should the Product Owner dictate what info the UI needs to display?
What does a straight horizontal line above a few notes, after a changed tempo mean?
OperationalError: sub-select returns 2 columns - expected 1 in Django QuerySet
How to combine 2 or more querysets in a Django view?How do I do a not equal in Django queryset filtering?django - convert a list back to a querysetGetting the SQL from a Django QuerySetDjango queryset count with extra selectDjango queryset .count() .filter() not working on custom model managerDjango, How to make multiple annotate in a single querysetDjango filter a reverse relationship of each item in a querysetAnnotate not returning the expected resultsDjango queryset for multilevel Foreignkey reference
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
There are two models a Price model and Service model. I'm trying to find the Services that have the most prices. All of this is initially filtered by a user input query (called the entry_query). The first line (1) get the price objects that the user queries (this works). Then, line 2 (2) returns a QS with the service_code and counts of prices a service has. Then, it brings on line (3) where it gives an error (see below).
(1) Price_objs = Price_filter.filter(entry_query)
(2) objs_filter=Price_objs.values_list('service__code').annotate(service_count=Count('service__code')).order_by('-service_count')
(3) serv_obj = Service.objects.filter(price__in = objs_filter).distinct()
Here is what line (2) outputs:
<QuerySet [('36430', 62), ('86003', 28), ('87149', 28), ('83516', 23), ('86317', 20), ('94640', 19), ('73502', 18), ('86658', 14), ('73721', 13), ('87070', 13), ('76942', 12), ('87081', 12), ('73560', 11), ('87798', 11), ('36415', 10), ('74177', 10), ('99211', 10), ('73100', 9), ('73221', 9), ('74176', 9), '...(remaining elements truncated)...']>
Here is the error that I get with line (3):
django.db.utils.OperationalError: sub-select returns 2 columns - expected 1
django django-queryset
add a comment |
There are two models a Price model and Service model. I'm trying to find the Services that have the most prices. All of this is initially filtered by a user input query (called the entry_query). The first line (1) get the price objects that the user queries (this works). Then, line 2 (2) returns a QS with the service_code and counts of prices a service has. Then, it brings on line (3) where it gives an error (see below).
(1) Price_objs = Price_filter.filter(entry_query)
(2) objs_filter=Price_objs.values_list('service__code').annotate(service_count=Count('service__code')).order_by('-service_count')
(3) serv_obj = Service.objects.filter(price__in = objs_filter).distinct()
Here is what line (2) outputs:
<QuerySet [('36430', 62), ('86003', 28), ('87149', 28), ('83516', 23), ('86317', 20), ('94640', 19), ('73502', 18), ('86658', 14), ('73721', 13), ('87070', 13), ('76942', 12), ('87081', 12), ('73560', 11), ('87798', 11), ('36415', 10), ('74177', 10), ('99211', 10), ('73100', 9), ('73221', 9), ('74176', 9), '...(remaining elements truncated)...']>
Here is the error that I get with line (3):
django.db.utils.OperationalError: sub-select returns 2 columns - expected 1
django django-queryset
1
I don't understand what you are doing here. Why are you annotating the service_count on the Price_objs query?
– Daniel Roseman
Mar 22 at 16:51
The price table has prices for services. I want to count how many prices there are for one service code - like service code '36430' has 62 prices. I want to dsiplay the Services with the most prices, so I want to order Service results by the number of prices that are available.
– J R
Mar 22 at 17:05
2
The other problem you have is, ordering thePrice
sub-select won't order theService
queryset. You should probably start fromService
and annotateprice__...
as related lookups.Service.objects.filter(price__...entry_query...).annotate(price_count=Count('price_set').order_by('-price_count')
Or something similar to this.
– Geekfish
Mar 22 at 17:20
This is the answer that I was looking for thanks. It worked.
– J R
Mar 23 at 2:53
add a comment |
There are two models a Price model and Service model. I'm trying to find the Services that have the most prices. All of this is initially filtered by a user input query (called the entry_query). The first line (1) get the price objects that the user queries (this works). Then, line 2 (2) returns a QS with the service_code and counts of prices a service has. Then, it brings on line (3) where it gives an error (see below).
(1) Price_objs = Price_filter.filter(entry_query)
(2) objs_filter=Price_objs.values_list('service__code').annotate(service_count=Count('service__code')).order_by('-service_count')
(3) serv_obj = Service.objects.filter(price__in = objs_filter).distinct()
Here is what line (2) outputs:
<QuerySet [('36430', 62), ('86003', 28), ('87149', 28), ('83516', 23), ('86317', 20), ('94640', 19), ('73502', 18), ('86658', 14), ('73721', 13), ('87070', 13), ('76942', 12), ('87081', 12), ('73560', 11), ('87798', 11), ('36415', 10), ('74177', 10), ('99211', 10), ('73100', 9), ('73221', 9), ('74176', 9), '...(remaining elements truncated)...']>
Here is the error that I get with line (3):
django.db.utils.OperationalError: sub-select returns 2 columns - expected 1
django django-queryset
There are two models a Price model and Service model. I'm trying to find the Services that have the most prices. All of this is initially filtered by a user input query (called the entry_query). The first line (1) get the price objects that the user queries (this works). Then, line 2 (2) returns a QS with the service_code and counts of prices a service has. Then, it brings on line (3) where it gives an error (see below).
(1) Price_objs = Price_filter.filter(entry_query)
(2) objs_filter=Price_objs.values_list('service__code').annotate(service_count=Count('service__code')).order_by('-service_count')
(3) serv_obj = Service.objects.filter(price__in = objs_filter).distinct()
Here is what line (2) outputs:
<QuerySet [('36430', 62), ('86003', 28), ('87149', 28), ('83516', 23), ('86317', 20), ('94640', 19), ('73502', 18), ('86658', 14), ('73721', 13), ('87070', 13), ('76942', 12), ('87081', 12), ('73560', 11), ('87798', 11), ('36415', 10), ('74177', 10), ('99211', 10), ('73100', 9), ('73221', 9), ('74176', 9), '...(remaining elements truncated)...']>
Here is the error that I get with line (3):
django.db.utils.OperationalError: sub-select returns 2 columns - expected 1
django django-queryset
django django-queryset
edited Mar 22 at 17:07
J R
asked Mar 22 at 16:48
J RJ R
6318
6318
1
I don't understand what you are doing here. Why are you annotating the service_count on the Price_objs query?
– Daniel Roseman
Mar 22 at 16:51
The price table has prices for services. I want to count how many prices there are for one service code - like service code '36430' has 62 prices. I want to dsiplay the Services with the most prices, so I want to order Service results by the number of prices that are available.
– J R
Mar 22 at 17:05
2
The other problem you have is, ordering thePrice
sub-select won't order theService
queryset. You should probably start fromService
and annotateprice__...
as related lookups.Service.objects.filter(price__...entry_query...).annotate(price_count=Count('price_set').order_by('-price_count')
Or something similar to this.
– Geekfish
Mar 22 at 17:20
This is the answer that I was looking for thanks. It worked.
– J R
Mar 23 at 2:53
add a comment |
1
I don't understand what you are doing here. Why are you annotating the service_count on the Price_objs query?
– Daniel Roseman
Mar 22 at 16:51
The price table has prices for services. I want to count how many prices there are for one service code - like service code '36430' has 62 prices. I want to dsiplay the Services with the most prices, so I want to order Service results by the number of prices that are available.
– J R
Mar 22 at 17:05
2
The other problem you have is, ordering thePrice
sub-select won't order theService
queryset. You should probably start fromService
and annotateprice__...
as related lookups.Service.objects.filter(price__...entry_query...).annotate(price_count=Count('price_set').order_by('-price_count')
Or something similar to this.
– Geekfish
Mar 22 at 17:20
This is the answer that I was looking for thanks. It worked.
– J R
Mar 23 at 2:53
1
1
I don't understand what you are doing here. Why are you annotating the service_count on the Price_objs query?
– Daniel Roseman
Mar 22 at 16:51
I don't understand what you are doing here. Why are you annotating the service_count on the Price_objs query?
– Daniel Roseman
Mar 22 at 16:51
The price table has prices for services. I want to count how many prices there are for one service code - like service code '36430' has 62 prices. I want to dsiplay the Services with the most prices, so I want to order Service results by the number of prices that are available.
– J R
Mar 22 at 17:05
The price table has prices for services. I want to count how many prices there are for one service code - like service code '36430' has 62 prices. I want to dsiplay the Services with the most prices, so I want to order Service results by the number of prices that are available.
– J R
Mar 22 at 17:05
2
2
The other problem you have is, ordering the
Price
sub-select won't order the Service
queryset. You should probably start from Service
and annotate price__...
as related lookups. Service.objects.filter(price__...entry_query...).annotate(price_count=Count('price_set').order_by('-price_count')
Or something similar to this.– Geekfish
Mar 22 at 17:20
The other problem you have is, ordering the
Price
sub-select won't order the Service
queryset. You should probably start from Service
and annotate price__...
as related lookups. Service.objects.filter(price__...entry_query...).annotate(price_count=Count('price_set').order_by('-price_count')
Or something similar to this.– Geekfish
Mar 22 at 17:20
This is the answer that I was looking for thanks. It worked.
– J R
Mar 23 at 2:53
This is the answer that I was looking for thanks. It worked.
– J R
Mar 23 at 2:53
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304323%2foperationalerror-sub-select-returns-2-columns-expected-1-in-django-queryset%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304323%2foperationalerror-sub-select-returns-2-columns-expected-1-in-django-queryset%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I don't understand what you are doing here. Why are you annotating the service_count on the Price_objs query?
– Daniel Roseman
Mar 22 at 16:51
The price table has prices for services. I want to count how many prices there are for one service code - like service code '36430' has 62 prices. I want to dsiplay the Services with the most prices, so I want to order Service results by the number of prices that are available.
– J R
Mar 22 at 17:05
2
The other problem you have is, ordering the
Price
sub-select won't order theService
queryset. You should probably start fromService
and annotateprice__...
as related lookups.Service.objects.filter(price__...entry_query...).annotate(price_count=Count('price_set').order_by('-price_count')
Or something similar to this.– Geekfish
Mar 22 at 17:20
This is the answer that I was looking for thanks. It worked.
– J R
Mar 23 at 2:53