How can I use prefetch_related in self related model












5















I have Menu model which has ForeignKey named parent related with itself.



If parent is None it means this menu is parent menu if it shows another Menu object it means it is submenu for its parent(many-to-one relation)



Here is my problem, I want to get all menus with its submenus using prefetch_related, How can I do it?




Note: I do not want to get submenus going database each time in for
menu




Here is my model class



class Menu(models.Model):
title = models.CharField(max_length=30)
language = models.ForeignKey(Language)
parent = models.ForeignKey("self", default=None, blank=True, null=True, related_name="submenus")


Here is my query



pm2 = Menu.objects.filter(parent=None, language__code=language, menutype=menutype).prefetch_related("submenus").order_by("order")
for p in pm2:
print(p.title)
print(p.submenus)


When I print the submenus the result is app.Menu.None










share|improve this question

























  • Your model doesn't mention submenus at all, so I would expect .prefetch_related("submenus") to give an error. Will your submenus have their own submenus, or will your menus only have two levels? If you have multiple levels, then you may want to look at django-mptt.

    – Alasdair
    Feb 26 '18 at 15:41













  • django-mptt is hopelessly convoluted to work with. I would suggest using materialized paths instead (e.g. communities.bmc.com/docs/DOC-9902)

    – thebjorn
    Feb 26 '18 at 15:54
















5















I have Menu model which has ForeignKey named parent related with itself.



If parent is None it means this menu is parent menu if it shows another Menu object it means it is submenu for its parent(many-to-one relation)



Here is my problem, I want to get all menus with its submenus using prefetch_related, How can I do it?




Note: I do not want to get submenus going database each time in for
menu




Here is my model class



class Menu(models.Model):
title = models.CharField(max_length=30)
language = models.ForeignKey(Language)
parent = models.ForeignKey("self", default=None, blank=True, null=True, related_name="submenus")


Here is my query



pm2 = Menu.objects.filter(parent=None, language__code=language, menutype=menutype).prefetch_related("submenus").order_by("order")
for p in pm2:
print(p.title)
print(p.submenus)


When I print the submenus the result is app.Menu.None










share|improve this question

























  • Your model doesn't mention submenus at all, so I would expect .prefetch_related("submenus") to give an error. Will your submenus have their own submenus, or will your menus only have two levels? If you have multiple levels, then you may want to look at django-mptt.

    – Alasdair
    Feb 26 '18 at 15:41













  • django-mptt is hopelessly convoluted to work with. I would suggest using materialized paths instead (e.g. communities.bmc.com/docs/DOC-9902)

    – thebjorn
    Feb 26 '18 at 15:54














5












5








5








I have Menu model which has ForeignKey named parent related with itself.



If parent is None it means this menu is parent menu if it shows another Menu object it means it is submenu for its parent(many-to-one relation)



Here is my problem, I want to get all menus with its submenus using prefetch_related, How can I do it?




Note: I do not want to get submenus going database each time in for
menu




Here is my model class



class Menu(models.Model):
title = models.CharField(max_length=30)
language = models.ForeignKey(Language)
parent = models.ForeignKey("self", default=None, blank=True, null=True, related_name="submenus")


Here is my query



pm2 = Menu.objects.filter(parent=None, language__code=language, menutype=menutype).prefetch_related("submenus").order_by("order")
for p in pm2:
print(p.title)
print(p.submenus)


When I print the submenus the result is app.Menu.None










share|improve this question
















I have Menu model which has ForeignKey named parent related with itself.



If parent is None it means this menu is parent menu if it shows another Menu object it means it is submenu for its parent(many-to-one relation)



Here is my problem, I want to get all menus with its submenus using prefetch_related, How can I do it?




Note: I do not want to get submenus going database each time in for
menu




Here is my model class



class Menu(models.Model):
title = models.CharField(max_length=30)
language = models.ForeignKey(Language)
parent = models.ForeignKey("self", default=None, blank=True, null=True, related_name="submenus")


Here is my query



pm2 = Menu.objects.filter(parent=None, language__code=language, menutype=menutype).prefetch_related("submenus").order_by("order")
for p in pm2:
print(p.title)
print(p.submenus)


When I print the submenus the result is app.Menu.None







django django-models django-orm






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 27 '18 at 7:43







Selim Yılmaz

















asked Feb 26 '18 at 15:30









Selim YılmazSelim Yılmaz

23627




23627













  • Your model doesn't mention submenus at all, so I would expect .prefetch_related("submenus") to give an error. Will your submenus have their own submenus, or will your menus only have two levels? If you have multiple levels, then you may want to look at django-mptt.

    – Alasdair
    Feb 26 '18 at 15:41













  • django-mptt is hopelessly convoluted to work with. I would suggest using materialized paths instead (e.g. communities.bmc.com/docs/DOC-9902)

    – thebjorn
    Feb 26 '18 at 15:54



















  • Your model doesn't mention submenus at all, so I would expect .prefetch_related("submenus") to give an error. Will your submenus have their own submenus, or will your menus only have two levels? If you have multiple levels, then you may want to look at django-mptt.

    – Alasdair
    Feb 26 '18 at 15:41













  • django-mptt is hopelessly convoluted to work with. I would suggest using materialized paths instead (e.g. communities.bmc.com/docs/DOC-9902)

    – thebjorn
    Feb 26 '18 at 15:54

















Your model doesn't mention submenus at all, so I would expect .prefetch_related("submenus") to give an error. Will your submenus have their own submenus, or will your menus only have two levels? If you have multiple levels, then you may want to look at django-mptt.

– Alasdair
Feb 26 '18 at 15:41







Your model doesn't mention submenus at all, so I would expect .prefetch_related("submenus") to give an error. Will your submenus have their own submenus, or will your menus only have two levels? If you have multiple levels, then you may want to look at django-mptt.

– Alasdair
Feb 26 '18 at 15:41















django-mptt is hopelessly convoluted to work with. I would suggest using materialized paths instead (e.g. communities.bmc.com/docs/DOC-9902)

– thebjorn
Feb 26 '18 at 15:54





django-mptt is hopelessly convoluted to work with. I would suggest using materialized paths instead (e.g. communities.bmc.com/docs/DOC-9902)

– thebjorn
Feb 26 '18 at 15:54












1 Answer
1






active

oldest

votes


















0














You don't need to use prefetch_related since this is used for many-to-many relationships, instead, you can use select_related.



So your query would be



pm2 = Menu.objects.filter(parent=None, language__code=language, menutype=menutype).select_related("submenus").order_by("order")





share|improve this answer
























  • Sorey for late response. In here i think i need to use prefetch_related because i want to get submenus and submenus is the reverse ForeignKeys "parent" field.

    – Selim Yılmaz
    Dec 28 '18 at 8:49











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48991847%2fhow-can-i-use-prefetch-related-in-self-related-model%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









0














You don't need to use prefetch_related since this is used for many-to-many relationships, instead, you can use select_related.



So your query would be



pm2 = Menu.objects.filter(parent=None, language__code=language, menutype=menutype).select_related("submenus").order_by("order")





share|improve this answer
























  • Sorey for late response. In here i think i need to use prefetch_related because i want to get submenus and submenus is the reverse ForeignKeys "parent" field.

    – Selim Yılmaz
    Dec 28 '18 at 8:49
















0














You don't need to use prefetch_related since this is used for many-to-many relationships, instead, you can use select_related.



So your query would be



pm2 = Menu.objects.filter(parent=None, language__code=language, menutype=menutype).select_related("submenus").order_by("order")





share|improve this answer
























  • Sorey for late response. In here i think i need to use prefetch_related because i want to get submenus and submenus is the reverse ForeignKeys "parent" field.

    – Selim Yılmaz
    Dec 28 '18 at 8:49














0












0








0







You don't need to use prefetch_related since this is used for many-to-many relationships, instead, you can use select_related.



So your query would be



pm2 = Menu.objects.filter(parent=None, language__code=language, menutype=menutype).select_related("submenus").order_by("order")





share|improve this answer













You don't need to use prefetch_related since this is used for many-to-many relationships, instead, you can use select_related.



So your query would be



pm2 = Menu.objects.filter(parent=None, language__code=language, menutype=menutype).select_related("submenus").order_by("order")






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 0:14









RadaRada

711315




711315













  • Sorey for late response. In here i think i need to use prefetch_related because i want to get submenus and submenus is the reverse ForeignKeys "parent" field.

    – Selim Yılmaz
    Dec 28 '18 at 8:49



















  • Sorey for late response. In here i think i need to use prefetch_related because i want to get submenus and submenus is the reverse ForeignKeys "parent" field.

    – Selim Yılmaz
    Dec 28 '18 at 8:49

















Sorey for late response. In here i think i need to use prefetch_related because i want to get submenus and submenus is the reverse ForeignKeys "parent" field.

– Selim Yılmaz
Dec 28 '18 at 8:49





Sorey for late response. In here i think i need to use prefetch_related because i want to get submenus and submenus is the reverse ForeignKeys "parent" field.

– Selim Yılmaz
Dec 28 '18 at 8:49




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48991847%2fhow-can-i-use-prefetch-related-in-self-related-model%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python