What is the usage of `FilteredRelation()` objects in Django ORM (Django 2.X)?What is a “slug” in Django?How do I do a not equal in Django queryset filtering?Does Django scale?How to view corresponding SQL query of the Django ORM's queryset?usage of iterator() on django querysetWhat's the difference between django OneToOneField and ForeignKey?differentiate null=True, blank=True in djangoExtentions of the Django docs' pizza example (screen for pizzas by topping)What's the difference between select_related and prefetch_related in Django ORM?Aggregation of an annotation in GROUP BY in Django
How can I know what hashing algorithm SQL Server used to decrypt the encrypted data when using the function DECRYPTBYPASSPHRASE?
Why is Sojdlg123aljg a common password?
Did the Byzantines ever attempt to move their capital to Rome?
Poor management handling of recent sickness and how to approach my return?
Is mountain bike good for long distances?
Why did Boris Johnson call for new elections?
How do draw effects during the discard phase work?
What's this inadvertent thing?
What makes an ending "happy"?
Get a MPS file using NEOS/GAMS web interface
Is Sanskrit really the mother of all languages?
Why are there no wireless switches?
Compiler optimization of bitwise not operation
Round away from zero
Can you pop microwave popcorn on a stove?
After a few interviews, What should I do after told to wait?
Contractor cut joist hangers to make them fit
Are fast interviews red flags?
Should I tip on the Amtrak train?
Python reimplementation of Lost In Space by Tim Hartnell
Draw the ☣ (Biohazard Symbol)
Can taking my 1-week-old on a 6-7 hours journey in the car lead to medical complications?
How do you say "to hell with everything" in French?
I multiply the source, you (probably) multiply the output!
What is the usage of `FilteredRelation()` objects in Django ORM (Django 2.X)?
What is a “slug” in Django?How do I do a not equal in Django queryset filtering?Does Django scale?How to view corresponding SQL query of the Django ORM's queryset?usage of iterator() on django querysetWhat's the difference between django OneToOneField and ForeignKey?differentiate null=True, blank=True in djangoExtentions of the Django docs' pizza example (screen for pizzas by topping)What's the difference between select_related and prefetch_related in Django ORM?Aggregation of an annotation in GROUP BY in Django
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I've seen Django 2.0 consists of FilteredRelation
object in queryset. What is the usage of newly introduced FilteredRelation
?
What I've looked into?
I observed Django 2.0 Documentation but I could not understand idea behind this FilteredRelation
object.
I looked into following code. But I didn't get it.
>>> from django.db.models import FilteredRelation, Q
>>> Restaurant.objects.annotate(
... pizzas_vegetarian=FilteredRelation(
... 'pizzas', condition=Q(pizzas__vegetarian=True),
... ),
... ).filter(pizzas_vegetarian__name__icontains='mozzarella')
Main Question
Show now my question is that what is usage of
FilteredRelation
and when to use in yourQuerySet
?
django django-models django-orm
add a comment |
I've seen Django 2.0 consists of FilteredRelation
object in queryset. What is the usage of newly introduced FilteredRelation
?
What I've looked into?
I observed Django 2.0 Documentation but I could not understand idea behind this FilteredRelation
object.
I looked into following code. But I didn't get it.
>>> from django.db.models import FilteredRelation, Q
>>> Restaurant.objects.annotate(
... pizzas_vegetarian=FilteredRelation(
... 'pizzas', condition=Q(pizzas__vegetarian=True),
... ),
... ).filter(pizzas_vegetarian__name__icontains='mozzarella')
Main Question
Show now my question is that what is usage of
FilteredRelation
and when to use in yourQuerySet
?
django django-models django-orm
add a comment |
I've seen Django 2.0 consists of FilteredRelation
object in queryset. What is the usage of newly introduced FilteredRelation
?
What I've looked into?
I observed Django 2.0 Documentation but I could not understand idea behind this FilteredRelation
object.
I looked into following code. But I didn't get it.
>>> from django.db.models import FilteredRelation, Q
>>> Restaurant.objects.annotate(
... pizzas_vegetarian=FilteredRelation(
... 'pizzas', condition=Q(pizzas__vegetarian=True),
... ),
... ).filter(pizzas_vegetarian__name__icontains='mozzarella')
Main Question
Show now my question is that what is usage of
FilteredRelation
and when to use in yourQuerySet
?
django django-models django-orm
I've seen Django 2.0 consists of FilteredRelation
object in queryset. What is the usage of newly introduced FilteredRelation
?
What I've looked into?
I observed Django 2.0 Documentation but I could not understand idea behind this FilteredRelation
object.
I looked into following code. But I didn't get it.
>>> from django.db.models import FilteredRelation, Q
>>> Restaurant.objects.annotate(
... pizzas_vegetarian=FilteredRelation(
... 'pizzas', condition=Q(pizzas__vegetarian=True),
... ),
... ).filter(pizzas_vegetarian__name__icontains='mozzarella')
Main Question
Show now my question is that what is usage of
FilteredRelation
and when to use in yourQuerySet
?
django django-models django-orm
django django-models django-orm
asked Mar 28 at 6:13
Devang PadhiyarDevang Padhiyar
1,2448 silver badges24 bronze badges
1,2448 silver badges24 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I think the documentation itself self-explanatory.
You could achieve the same result in,
Method-1
from django.db.models import FilteredRelation, Q
result_1 = Restaurant.objects.annotate(pizzas_vegetarian=FilteredRelation('pizzas', condition=Q(pizzas__vegetarian=True), ), ).filter(
pizzas_vegetarian__name__icontains='mozzarella')
Method-2
result_2 = Restaurant.objects.filter(pizzas__vegetarian=True, pizzas__name__icontains='mozzarella')
You will get better performance with Method-1 since the filtering in the WHERE clause of the first queryset will only operate on vegetarian pizzas.
UPDATE
The Django #29555 ticket has more information regarding the usage and performance.
The FilteredRelation()
not only improves performance but also creates
correct results when aggregating with multiple LEFT JOIN
s.
Does both of the query returns same results?
– Devang Padhiyar
Mar 28 at 6:32
Yes. But, you can't useFilteredRelation
always because it has some restrictions. (specified on FilteredRelation doesn’t support section of the doc)
– JPG
Mar 28 at 6:34
Should I use F('field_name') within condition
– Devang Padhiyar
Mar 28 at 8:39
Nop, because anF()
object represents the value of a model field or annotated column.
– JPG
Mar 28 at 8:40
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/4.0/"u003ecc by-sa 4.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%2f55391181%2fwhat-is-the-usage-of-filteredrelation-objects-in-django-orm-django-2-x%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think the documentation itself self-explanatory.
You could achieve the same result in,
Method-1
from django.db.models import FilteredRelation, Q
result_1 = Restaurant.objects.annotate(pizzas_vegetarian=FilteredRelation('pizzas', condition=Q(pizzas__vegetarian=True), ), ).filter(
pizzas_vegetarian__name__icontains='mozzarella')
Method-2
result_2 = Restaurant.objects.filter(pizzas__vegetarian=True, pizzas__name__icontains='mozzarella')
You will get better performance with Method-1 since the filtering in the WHERE clause of the first queryset will only operate on vegetarian pizzas.
UPDATE
The Django #29555 ticket has more information regarding the usage and performance.
The FilteredRelation()
not only improves performance but also creates
correct results when aggregating with multiple LEFT JOIN
s.
Does both of the query returns same results?
– Devang Padhiyar
Mar 28 at 6:32
Yes. But, you can't useFilteredRelation
always because it has some restrictions. (specified on FilteredRelation doesn’t support section of the doc)
– JPG
Mar 28 at 6:34
Should I use F('field_name') within condition
– Devang Padhiyar
Mar 28 at 8:39
Nop, because anF()
object represents the value of a model field or annotated column.
– JPG
Mar 28 at 8:40
add a comment |
I think the documentation itself self-explanatory.
You could achieve the same result in,
Method-1
from django.db.models import FilteredRelation, Q
result_1 = Restaurant.objects.annotate(pizzas_vegetarian=FilteredRelation('pizzas', condition=Q(pizzas__vegetarian=True), ), ).filter(
pizzas_vegetarian__name__icontains='mozzarella')
Method-2
result_2 = Restaurant.objects.filter(pizzas__vegetarian=True, pizzas__name__icontains='mozzarella')
You will get better performance with Method-1 since the filtering in the WHERE clause of the first queryset will only operate on vegetarian pizzas.
UPDATE
The Django #29555 ticket has more information regarding the usage and performance.
The FilteredRelation()
not only improves performance but also creates
correct results when aggregating with multiple LEFT JOIN
s.
Does both of the query returns same results?
– Devang Padhiyar
Mar 28 at 6:32
Yes. But, you can't useFilteredRelation
always because it has some restrictions. (specified on FilteredRelation doesn’t support section of the doc)
– JPG
Mar 28 at 6:34
Should I use F('field_name') within condition
– Devang Padhiyar
Mar 28 at 8:39
Nop, because anF()
object represents the value of a model field or annotated column.
– JPG
Mar 28 at 8:40
add a comment |
I think the documentation itself self-explanatory.
You could achieve the same result in,
Method-1
from django.db.models import FilteredRelation, Q
result_1 = Restaurant.objects.annotate(pizzas_vegetarian=FilteredRelation('pizzas', condition=Q(pizzas__vegetarian=True), ), ).filter(
pizzas_vegetarian__name__icontains='mozzarella')
Method-2
result_2 = Restaurant.objects.filter(pizzas__vegetarian=True, pizzas__name__icontains='mozzarella')
You will get better performance with Method-1 since the filtering in the WHERE clause of the first queryset will only operate on vegetarian pizzas.
UPDATE
The Django #29555 ticket has more information regarding the usage and performance.
The FilteredRelation()
not only improves performance but also creates
correct results when aggregating with multiple LEFT JOIN
s.
I think the documentation itself self-explanatory.
You could achieve the same result in,
Method-1
from django.db.models import FilteredRelation, Q
result_1 = Restaurant.objects.annotate(pizzas_vegetarian=FilteredRelation('pizzas', condition=Q(pizzas__vegetarian=True), ), ).filter(
pizzas_vegetarian__name__icontains='mozzarella')
Method-2
result_2 = Restaurant.objects.filter(pizzas__vegetarian=True, pizzas__name__icontains='mozzarella')
You will get better performance with Method-1 since the filtering in the WHERE clause of the first queryset will only operate on vegetarian pizzas.
UPDATE
The Django #29555 ticket has more information regarding the usage and performance.
The FilteredRelation()
not only improves performance but also creates
correct results when aggregating with multiple LEFT JOIN
s.
edited Mar 28 at 8:37
answered Mar 28 at 6:30
JPGJPG
22.4k3 gold badges15 silver badges47 bronze badges
22.4k3 gold badges15 silver badges47 bronze badges
Does both of the query returns same results?
– Devang Padhiyar
Mar 28 at 6:32
Yes. But, you can't useFilteredRelation
always because it has some restrictions. (specified on FilteredRelation doesn’t support section of the doc)
– JPG
Mar 28 at 6:34
Should I use F('field_name') within condition
– Devang Padhiyar
Mar 28 at 8:39
Nop, because anF()
object represents the value of a model field or annotated column.
– JPG
Mar 28 at 8:40
add a comment |
Does both of the query returns same results?
– Devang Padhiyar
Mar 28 at 6:32
Yes. But, you can't useFilteredRelation
always because it has some restrictions. (specified on FilteredRelation doesn’t support section of the doc)
– JPG
Mar 28 at 6:34
Should I use F('field_name') within condition
– Devang Padhiyar
Mar 28 at 8:39
Nop, because anF()
object represents the value of a model field or annotated column.
– JPG
Mar 28 at 8:40
Does both of the query returns same results?
– Devang Padhiyar
Mar 28 at 6:32
Does both of the query returns same results?
– Devang Padhiyar
Mar 28 at 6:32
Yes. But, you can't use
FilteredRelation
always because it has some restrictions. (specified on FilteredRelation doesn’t support section of the doc)– JPG
Mar 28 at 6:34
Yes. But, you can't use
FilteredRelation
always because it has some restrictions. (specified on FilteredRelation doesn’t support section of the doc)– JPG
Mar 28 at 6:34
Should I use F('field_name') within condition
– Devang Padhiyar
Mar 28 at 8:39
Should I use F('field_name') within condition
– Devang Padhiyar
Mar 28 at 8:39
Nop, because an
F()
object represents the value of a model field or annotated column.– JPG
Mar 28 at 8:40
Nop, because an
F()
object represents the value of a model field or annotated column.– JPG
Mar 28 at 8:40
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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%2f55391181%2fwhat-is-the-usage-of-filteredrelation-objects-in-django-orm-django-2-x%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