Filtering by Field on Foreign Key
I've got two models that are related to one another
class IndustryService(models.Model):
title = models.CharField(max_length=120)
pricingisarate = models.BooleanField(default=False)
class UserService(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.ForeignKey(IndustryService, on_delete=models.CASCADE, null=True, blank=True)
Within a view, I'm trying to develop a queryset of UserService instances that
a) belongs to a user
b) on the foreign key, has pricingisarate == True
I've tried the following query, but it doesn't work:
services = UserService.objects.filter(user=user, industryservice__pricingisarate__is=True)
Thanks for your help!!!
python django django-models django-views
add a comment |
I've got two models that are related to one another
class IndustryService(models.Model):
title = models.CharField(max_length=120)
pricingisarate = models.BooleanField(default=False)
class UserService(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.ForeignKey(IndustryService, on_delete=models.CASCADE, null=True, blank=True)
Within a view, I'm trying to develop a queryset of UserService instances that
a) belongs to a user
b) on the foreign key, has pricingisarate == True
I've tried the following query, but it doesn't work:
services = UserService.objects.filter(user=user, industryservice__pricingisarate__is=True)
Thanks for your help!!!
python django django-models django-views
No, sorry. This query doesn't work either.
– Jason Howard
Dec 30 '18 at 9:28
add a comment |
I've got two models that are related to one another
class IndustryService(models.Model):
title = models.CharField(max_length=120)
pricingisarate = models.BooleanField(default=False)
class UserService(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.ForeignKey(IndustryService, on_delete=models.CASCADE, null=True, blank=True)
Within a view, I'm trying to develop a queryset of UserService instances that
a) belongs to a user
b) on the foreign key, has pricingisarate == True
I've tried the following query, but it doesn't work:
services = UserService.objects.filter(user=user, industryservice__pricingisarate__is=True)
Thanks for your help!!!
python django django-models django-views
I've got two models that are related to one another
class IndustryService(models.Model):
title = models.CharField(max_length=120)
pricingisarate = models.BooleanField(default=False)
class UserService(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.ForeignKey(IndustryService, on_delete=models.CASCADE, null=True, blank=True)
Within a view, I'm trying to develop a queryset of UserService instances that
a) belongs to a user
b) on the foreign key, has pricingisarate == True
I've tried the following query, but it doesn't work:
services = UserService.objects.filter(user=user, industryservice__pricingisarate__is=True)
Thanks for your help!!!
python django django-models django-views
python django django-models django-views
edited Dec 30 '18 at 9:41
Moh
3,79432542
3,79432542
asked Dec 30 '18 at 9:23
Jason HowardJason Howard
1719
1719
No, sorry. This query doesn't work either.
– Jason Howard
Dec 30 '18 at 9:28
add a comment |
No, sorry. This query doesn't work either.
– Jason Howard
Dec 30 '18 at 9:28
No, sorry. This query doesn't work either.
– Jason Howard
Dec 30 '18 at 9:28
No, sorry. This query doesn't work either.
– Jason Howard
Dec 30 '18 at 9:28
add a comment |
3 Answers
3
active
oldest
votes
Got it!
services = UserService.objects.filter(user=user, title__pricingisarate=True)
add a comment |
You can filtering Foreign-Keys fields by using double underline between foreign-key defined name and sub field name that you want filtering by this, for your case it is similar below:
title__pricingisarate
And your query must change as below:
services = UserService.objects.filter(user=user, title__pricingisarate=True)
Some formal examples of Django about this article is available...
add a comment |
services = UserService.objects.filter(user=user, title__pricingisarate=True)
Because UserService
is related to IndustryService
model using lookup title.
Please refer to this link - https://docs.djangoproject.com/en/2.1/topics/db/queries/#lookups-that-span-relationships
add a comment |
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%2f53976477%2ffiltering-by-field-on-foreign-key%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Got it!
services = UserService.objects.filter(user=user, title__pricingisarate=True)
add a comment |
Got it!
services = UserService.objects.filter(user=user, title__pricingisarate=True)
add a comment |
Got it!
services = UserService.objects.filter(user=user, title__pricingisarate=True)
Got it!
services = UserService.objects.filter(user=user, title__pricingisarate=True)
edited Dec 30 '18 at 9:43
Moh
3,79432542
3,79432542
answered Dec 30 '18 at 9:30
Jason HowardJason Howard
1719
1719
add a comment |
add a comment |
You can filtering Foreign-Keys fields by using double underline between foreign-key defined name and sub field name that you want filtering by this, for your case it is similar below:
title__pricingisarate
And your query must change as below:
services = UserService.objects.filter(user=user, title__pricingisarate=True)
Some formal examples of Django about this article is available...
add a comment |
You can filtering Foreign-Keys fields by using double underline between foreign-key defined name and sub field name that you want filtering by this, for your case it is similar below:
title__pricingisarate
And your query must change as below:
services = UserService.objects.filter(user=user, title__pricingisarate=True)
Some formal examples of Django about this article is available...
add a comment |
You can filtering Foreign-Keys fields by using double underline between foreign-key defined name and sub field name that you want filtering by this, for your case it is similar below:
title__pricingisarate
And your query must change as below:
services = UserService.objects.filter(user=user, title__pricingisarate=True)
Some formal examples of Django about this article is available...
You can filtering Foreign-Keys fields by using double underline between foreign-key defined name and sub field name that you want filtering by this, for your case it is similar below:
title__pricingisarate
And your query must change as below:
services = UserService.objects.filter(user=user, title__pricingisarate=True)
Some formal examples of Django about this article is available...
answered Dec 30 '18 at 9:46
MohMoh
3,79432542
3,79432542
add a comment |
add a comment |
services = UserService.objects.filter(user=user, title__pricingisarate=True)
Because UserService
is related to IndustryService
model using lookup title.
Please refer to this link - https://docs.djangoproject.com/en/2.1/topics/db/queries/#lookups-that-span-relationships
add a comment |
services = UserService.objects.filter(user=user, title__pricingisarate=True)
Because UserService
is related to IndustryService
model using lookup title.
Please refer to this link - https://docs.djangoproject.com/en/2.1/topics/db/queries/#lookups-that-span-relationships
add a comment |
services = UserService.objects.filter(user=user, title__pricingisarate=True)
Because UserService
is related to IndustryService
model using lookup title.
Please refer to this link - https://docs.djangoproject.com/en/2.1/topics/db/queries/#lookups-that-span-relationships
services = UserService.objects.filter(user=user, title__pricingisarate=True)
Because UserService
is related to IndustryService
model using lookup title.
Please refer to this link - https://docs.djangoproject.com/en/2.1/topics/db/queries/#lookups-that-span-relationships
edited Dec 30 '18 at 11:20
Loss of human identity
1,1571922
1,1571922
answered Dec 30 '18 at 9:59
user3775768user3775768
11
11
add a comment |
add a comment |
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%2f53976477%2ffiltering-by-field-on-foreign-key%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
No, sorry. This query doesn't work either.
– Jason Howard
Dec 30 '18 at 9:28