How to use ForeignKey.limit_choices_to to limit relation between two objects with the same parent object
I have the following django models:
class League(models.Model):
class Division(models.Model):
league = models.ForeignKey(League)
class Season(models.Model):
league = models.ForeignKey(League)
divisions = models.ManyToManyField(Division)
Is it possible to use ManyToManyField.limit_choices_to on Season.divisions to require that a season and division can only be related if they have the same league value?
According to the django documentation, limit_choices_to
Sets a limit to the available choices for this field when this field
is rendered using a ModelForm or the admin (by default, all objects in
the queryset are available to choose). Either a dictionary, a Q
object, or a callable returning a dictionary or Q object can be used.
Is there some way to use a Q() and use a reference to the current Season record? Something like the following does not work because self isn't defined, but perhaps there is some way of specifying something similar:
divisions = models.ManyToManyField(Division, limit_choices_to=Q( league = self.league )
django-models django-admin
add a comment |
I have the following django models:
class League(models.Model):
class Division(models.Model):
league = models.ForeignKey(League)
class Season(models.Model):
league = models.ForeignKey(League)
divisions = models.ManyToManyField(Division)
Is it possible to use ManyToManyField.limit_choices_to on Season.divisions to require that a season and division can only be related if they have the same league value?
According to the django documentation, limit_choices_to
Sets a limit to the available choices for this field when this field
is rendered using a ModelForm or the admin (by default, all objects in
the queryset are available to choose). Either a dictionary, a Q
object, or a callable returning a dictionary or Q object can be used.
Is there some way to use a Q() and use a reference to the current Season record? Something like the following does not work because self isn't defined, but perhaps there is some way of specifying something similar:
divisions = models.ManyToManyField(Division, limit_choices_to=Q( league = self.league )
django-models django-admin
Have you triedlimit_choices_to={'league': F('league')}
?
– Hugo Luis Villalobos Canto
Nov 22 '18 at 3:24
add a comment |
I have the following django models:
class League(models.Model):
class Division(models.Model):
league = models.ForeignKey(League)
class Season(models.Model):
league = models.ForeignKey(League)
divisions = models.ManyToManyField(Division)
Is it possible to use ManyToManyField.limit_choices_to on Season.divisions to require that a season and division can only be related if they have the same league value?
According to the django documentation, limit_choices_to
Sets a limit to the available choices for this field when this field
is rendered using a ModelForm or the admin (by default, all objects in
the queryset are available to choose). Either a dictionary, a Q
object, or a callable returning a dictionary or Q object can be used.
Is there some way to use a Q() and use a reference to the current Season record? Something like the following does not work because self isn't defined, but perhaps there is some way of specifying something similar:
divisions = models.ManyToManyField(Division, limit_choices_to=Q( league = self.league )
django-models django-admin
I have the following django models:
class League(models.Model):
class Division(models.Model):
league = models.ForeignKey(League)
class Season(models.Model):
league = models.ForeignKey(League)
divisions = models.ManyToManyField(Division)
Is it possible to use ManyToManyField.limit_choices_to on Season.divisions to require that a season and division can only be related if they have the same league value?
According to the django documentation, limit_choices_to
Sets a limit to the available choices for this field when this field
is rendered using a ModelForm or the admin (by default, all objects in
the queryset are available to choose). Either a dictionary, a Q
object, or a callable returning a dictionary or Q object can be used.
Is there some way to use a Q() and use a reference to the current Season record? Something like the following does not work because self isn't defined, but perhaps there is some way of specifying something similar:
divisions = models.ManyToManyField(Division, limit_choices_to=Q( league = self.league )
django-models django-admin
django-models django-admin
asked Nov 15 '18 at 21:49
mightyrosermightyroser
40547
40547
Have you triedlimit_choices_to={'league': F('league')}
?
– Hugo Luis Villalobos Canto
Nov 22 '18 at 3:24
add a comment |
Have you triedlimit_choices_to={'league': F('league')}
?
– Hugo Luis Villalobos Canto
Nov 22 '18 at 3:24
Have you tried
limit_choices_to={'league': F('league')}
?– Hugo Luis Villalobos Canto
Nov 22 '18 at 3:24
Have you tried
limit_choices_to={'league': F('league')}
?– Hugo Luis Villalobos Canto
Nov 22 '18 at 3:24
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%2f53328374%2fhow-to-use-foreignkey-limit-choices-to-to-limit-relation-between-two-objects-wit%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%2f53328374%2fhow-to-use-foreignkey-limit-choices-to-to-limit-relation-between-two-objects-wit%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
Have you tried
limit_choices_to={'league': F('league')}
?– Hugo Luis Villalobos Canto
Nov 22 '18 at 3:24