How to write joins in orm
class Test(models.Model):
name = models.CharField(max_length=50)
class TestDetails(models.Model):
user = models.ForeignKey(User,on_delete = models.CASCADE)
test = models.ForeignKey(Test,on_delete = models.CASCADE)
mark = models.IntegerField(default=0)
I create a TestDetails when a user signs up for a test. I need to get the list of tests that the user has not signed up.
django django-models
add a comment |
class Test(models.Model):
name = models.CharField(max_length=50)
class TestDetails(models.Model):
user = models.ForeignKey(User,on_delete = models.CASCADE)
test = models.ForeignKey(Test,on_delete = models.CASCADE)
mark = models.IntegerField(default=0)
I create a TestDetails when a user signs up for a test. I need to get the list of tests that the user has not signed up.
django django-models
1
TryTest.objects.exclude(testdetails__user=my_user)
– Selcuk
Nov 14 '18 at 4:30
1
@Selcuk Doesn't that assume arelated_name
that doesn't appear to be set onTestDetails.test
? I think the default would betest_details_set
in this case.
– kungphu
Nov 14 '18 at 4:34
1
@kungphu No,_set
convention generates a convenience property to access all child objects. In a query you should be using the actual model name. See docs.djangoproject.com/en/2.1/topics/db/examples/many_to_one for more examples; for exampleReporter.objects.filter(article__headline__startswith='This')
. Note that it isarticle__headline
and notarticle__set__headline
.
– Selcuk
Nov 14 '18 at 5:13
Well that's interesting. I can't say I like the fact that it usestestdetails
instead oftest_details
; seems a little counter to the way names are usually treated in Python and Django.
– kungphu
Nov 14 '18 at 6:59
@kungphu Agreed, it is a bit inconsistent but I guess the reason was to reduce the number of underscores in a long traversal, or it's just¯_(ツ)_/¯
.
– Selcuk
Nov 15 '18 at 3:34
add a comment |
class Test(models.Model):
name = models.CharField(max_length=50)
class TestDetails(models.Model):
user = models.ForeignKey(User,on_delete = models.CASCADE)
test = models.ForeignKey(Test,on_delete = models.CASCADE)
mark = models.IntegerField(default=0)
I create a TestDetails when a user signs up for a test. I need to get the list of tests that the user has not signed up.
django django-models
class Test(models.Model):
name = models.CharField(max_length=50)
class TestDetails(models.Model):
user = models.ForeignKey(User,on_delete = models.CASCADE)
test = models.ForeignKey(Test,on_delete = models.CASCADE)
mark = models.IntegerField(default=0)
I create a TestDetails when a user signs up for a test. I need to get the list of tests that the user has not signed up.
django django-models
django django-models
asked Nov 14 '18 at 4:17
Garry KevinGarry Kevin
114
114
1
TryTest.objects.exclude(testdetails__user=my_user)
– Selcuk
Nov 14 '18 at 4:30
1
@Selcuk Doesn't that assume arelated_name
that doesn't appear to be set onTestDetails.test
? I think the default would betest_details_set
in this case.
– kungphu
Nov 14 '18 at 4:34
1
@kungphu No,_set
convention generates a convenience property to access all child objects. In a query you should be using the actual model name. See docs.djangoproject.com/en/2.1/topics/db/examples/many_to_one for more examples; for exampleReporter.objects.filter(article__headline__startswith='This')
. Note that it isarticle__headline
and notarticle__set__headline
.
– Selcuk
Nov 14 '18 at 5:13
Well that's interesting. I can't say I like the fact that it usestestdetails
instead oftest_details
; seems a little counter to the way names are usually treated in Python and Django.
– kungphu
Nov 14 '18 at 6:59
@kungphu Agreed, it is a bit inconsistent but I guess the reason was to reduce the number of underscores in a long traversal, or it's just¯_(ツ)_/¯
.
– Selcuk
Nov 15 '18 at 3:34
add a comment |
1
TryTest.objects.exclude(testdetails__user=my_user)
– Selcuk
Nov 14 '18 at 4:30
1
@Selcuk Doesn't that assume arelated_name
that doesn't appear to be set onTestDetails.test
? I think the default would betest_details_set
in this case.
– kungphu
Nov 14 '18 at 4:34
1
@kungphu No,_set
convention generates a convenience property to access all child objects. In a query you should be using the actual model name. See docs.djangoproject.com/en/2.1/topics/db/examples/many_to_one for more examples; for exampleReporter.objects.filter(article__headline__startswith='This')
. Note that it isarticle__headline
and notarticle__set__headline
.
– Selcuk
Nov 14 '18 at 5:13
Well that's interesting. I can't say I like the fact that it usestestdetails
instead oftest_details
; seems a little counter to the way names are usually treated in Python and Django.
– kungphu
Nov 14 '18 at 6:59
@kungphu Agreed, it is a bit inconsistent but I guess the reason was to reduce the number of underscores in a long traversal, or it's just¯_(ツ)_/¯
.
– Selcuk
Nov 15 '18 at 3:34
1
1
Try
Test.objects.exclude(testdetails__user=my_user)
– Selcuk
Nov 14 '18 at 4:30
Try
Test.objects.exclude(testdetails__user=my_user)
– Selcuk
Nov 14 '18 at 4:30
1
1
@Selcuk Doesn't that assume a
related_name
that doesn't appear to be set on TestDetails.test
? I think the default would be test_details_set
in this case.– kungphu
Nov 14 '18 at 4:34
@Selcuk Doesn't that assume a
related_name
that doesn't appear to be set on TestDetails.test
? I think the default would be test_details_set
in this case.– kungphu
Nov 14 '18 at 4:34
1
1
@kungphu No,
_set
convention generates a convenience property to access all child objects. In a query you should be using the actual model name. See docs.djangoproject.com/en/2.1/topics/db/examples/many_to_one for more examples; for example Reporter.objects.filter(article__headline__startswith='This')
. Note that it is article__headline
and not article__set__headline
.– Selcuk
Nov 14 '18 at 5:13
@kungphu No,
_set
convention generates a convenience property to access all child objects. In a query you should be using the actual model name. See docs.djangoproject.com/en/2.1/topics/db/examples/many_to_one for more examples; for example Reporter.objects.filter(article__headline__startswith='This')
. Note that it is article__headline
and not article__set__headline
.– Selcuk
Nov 14 '18 at 5:13
Well that's interesting. I can't say I like the fact that it uses
testdetails
instead of test_details
; seems a little counter to the way names are usually treated in Python and Django.– kungphu
Nov 14 '18 at 6:59
Well that's interesting. I can't say I like the fact that it uses
testdetails
instead of test_details
; seems a little counter to the way names are usually treated in Python and Django.– kungphu
Nov 14 '18 at 6:59
@kungphu Agreed, it is a bit inconsistent but I guess the reason was to reduce the number of underscores in a long traversal, or it's just
¯_(ツ)_/¯
.– Selcuk
Nov 15 '18 at 3:34
@kungphu Agreed, it is a bit inconsistent but I guess the reason was to reduce the number of underscores in a long traversal, or it's just
¯_(ツ)_/¯
.– Selcuk
Nov 15 '18 at 3:34
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%2f53293126%2fhow-to-write-joins-in-orm%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%2f53293126%2fhow-to-write-joins-in-orm%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
Try
Test.objects.exclude(testdetails__user=my_user)
– Selcuk
Nov 14 '18 at 4:30
1
@Selcuk Doesn't that assume a
related_name
that doesn't appear to be set onTestDetails.test
? I think the default would betest_details_set
in this case.– kungphu
Nov 14 '18 at 4:34
1
@kungphu No,
_set
convention generates a convenience property to access all child objects. In a query you should be using the actual model name. See docs.djangoproject.com/en/2.1/topics/db/examples/many_to_one for more examples; for exampleReporter.objects.filter(article__headline__startswith='This')
. Note that it isarticle__headline
and notarticle__set__headline
.– Selcuk
Nov 14 '18 at 5:13
Well that's interesting. I can't say I like the fact that it uses
testdetails
instead oftest_details
; seems a little counter to the way names are usually treated in Python and Django.– kungphu
Nov 14 '18 at 6:59
@kungphu Agreed, it is a bit inconsistent but I guess the reason was to reduce the number of underscores in a long traversal, or it's just
¯_(ツ)_/¯
.– Selcuk
Nov 15 '18 at 3:34