Wagtail: Getting a list of snippets with counts of related parents
Using Wagtail I have ArticlePage
s, each of which can have 0 or more Author
s, which are Snippets.
I'd like to get a list of Author
s with the number of ArticlePage
s they're attached to, but can't work out how. I'm getting confused by ModelCluster I think, as I'd be fine with vanilla Django.
(I'm not even sure if I'm over-complicating this; I don't need the Authors to be orderable on the Articles...)
from django.db import models
from modelcluster.fields import ParentalKey
from wagtail.admin.edit_handlers import InlinePanel
from wagtail.core.models import Orderable, Page
from wagtail.snippets.edit_handlers import SnippetChooserPanel
from wagtail.snippets.models import register_snippet
class ArticlePage(Page):
content_panels = Page.content_panels + [
InlinePanel('authors', label='Authors'),
]
@register_snippet
class Author(models.Model):
name = models.CharField(max_length=255, blank=False)
panels = [
FieldPanel('name'),
]
class ArticleAuthorRelationship(Orderable, models.Model):
author = models.ForeignKey(
'Author',
on_delete=models.CASCADE,
related_name='+')
page = ParentalKey(
'ArticlePage',
on_delete=models.CASCADE,
related_name='authors')
panels = [
SnippetChooserPanel('author'),
]
FWIW, I know I could get a single Author
's articles using article.get_usage()
, which is handy!
django wagtail
add a comment |
Using Wagtail I have ArticlePage
s, each of which can have 0 or more Author
s, which are Snippets.
I'd like to get a list of Author
s with the number of ArticlePage
s they're attached to, but can't work out how. I'm getting confused by ModelCluster I think, as I'd be fine with vanilla Django.
(I'm not even sure if I'm over-complicating this; I don't need the Authors to be orderable on the Articles...)
from django.db import models
from modelcluster.fields import ParentalKey
from wagtail.admin.edit_handlers import InlinePanel
from wagtail.core.models import Orderable, Page
from wagtail.snippets.edit_handlers import SnippetChooserPanel
from wagtail.snippets.models import register_snippet
class ArticlePage(Page):
content_panels = Page.content_panels + [
InlinePanel('authors', label='Authors'),
]
@register_snippet
class Author(models.Model):
name = models.CharField(max_length=255, blank=False)
panels = [
FieldPanel('name'),
]
class ArticleAuthorRelationship(Orderable, models.Model):
author = models.ForeignKey(
'Author',
on_delete=models.CASCADE,
related_name='+')
page = ParentalKey(
'ArticlePage',
on_delete=models.CASCADE,
related_name='authors')
panels = [
SnippetChooserPanel('author'),
]
FWIW, I know I could get a single Author
's articles using article.get_usage()
, which is handy!
django wagtail
add a comment |
Using Wagtail I have ArticlePage
s, each of which can have 0 or more Author
s, which are Snippets.
I'd like to get a list of Author
s with the number of ArticlePage
s they're attached to, but can't work out how. I'm getting confused by ModelCluster I think, as I'd be fine with vanilla Django.
(I'm not even sure if I'm over-complicating this; I don't need the Authors to be orderable on the Articles...)
from django.db import models
from modelcluster.fields import ParentalKey
from wagtail.admin.edit_handlers import InlinePanel
from wagtail.core.models import Orderable, Page
from wagtail.snippets.edit_handlers import SnippetChooserPanel
from wagtail.snippets.models import register_snippet
class ArticlePage(Page):
content_panels = Page.content_panels + [
InlinePanel('authors', label='Authors'),
]
@register_snippet
class Author(models.Model):
name = models.CharField(max_length=255, blank=False)
panels = [
FieldPanel('name'),
]
class ArticleAuthorRelationship(Orderable, models.Model):
author = models.ForeignKey(
'Author',
on_delete=models.CASCADE,
related_name='+')
page = ParentalKey(
'ArticlePage',
on_delete=models.CASCADE,
related_name='authors')
panels = [
SnippetChooserPanel('author'),
]
FWIW, I know I could get a single Author
's articles using article.get_usage()
, which is handy!
django wagtail
Using Wagtail I have ArticlePage
s, each of which can have 0 or more Author
s, which are Snippets.
I'd like to get a list of Author
s with the number of ArticlePage
s they're attached to, but can't work out how. I'm getting confused by ModelCluster I think, as I'd be fine with vanilla Django.
(I'm not even sure if I'm over-complicating this; I don't need the Authors to be orderable on the Articles...)
from django.db import models
from modelcluster.fields import ParentalKey
from wagtail.admin.edit_handlers import InlinePanel
from wagtail.core.models import Orderable, Page
from wagtail.snippets.edit_handlers import SnippetChooserPanel
from wagtail.snippets.models import register_snippet
class ArticlePage(Page):
content_panels = Page.content_panels + [
InlinePanel('authors', label='Authors'),
]
@register_snippet
class Author(models.Model):
name = models.CharField(max_length=255, blank=False)
panels = [
FieldPanel('name'),
]
class ArticleAuthorRelationship(Orderable, models.Model):
author = models.ForeignKey(
'Author',
on_delete=models.CASCADE,
related_name='+')
page = ParentalKey(
'ArticlePage',
on_delete=models.CASCADE,
related_name='authors')
panels = [
SnippetChooserPanel('author'),
]
FWIW, I know I could get a single Author
's articles using article.get_usage()
, which is handy!
django wagtail
django wagtail
asked Nov 20 at 17:08
Phil Gyford
3,99653174
3,99653174
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Coming back to this a day later, I think the only issue is having set related_name
on ArticleAuthorRelationship
's author
property to '+'
(which means there's no backwards relationship from Author
to this model).
If I instead do:
class ArticleAuthorRelationship(Orderable, models.Model):
author = models.ForeignKey(
'Author',
on_delete=models.CASCADE,
related_name='articles') # Changed this to 'articles'
# etc.
Then I can do this quite easily:
from django.db.models import Count
from my app.models import Author
authors = Author.objects.annotate(num_articles=Count('articles')).order_by('-num_articles')
Then each item in the authors
QuerySet has a num_articles
property.
I usually set a related_name
but this seems less common in the Wagtail docs, and I almost hadn't noticed I'd copied that behaviour here, so this threw me.
If anyone's reading this and can give examples of why/when you'd want to set related_name
to '+'
, I'd be interested to know. Thanks.
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%2f53398063%2fwagtail-getting-a-list-of-snippets-with-counts-of-related-parents%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
Coming back to this a day later, I think the only issue is having set related_name
on ArticleAuthorRelationship
's author
property to '+'
(which means there's no backwards relationship from Author
to this model).
If I instead do:
class ArticleAuthorRelationship(Orderable, models.Model):
author = models.ForeignKey(
'Author',
on_delete=models.CASCADE,
related_name='articles') # Changed this to 'articles'
# etc.
Then I can do this quite easily:
from django.db.models import Count
from my app.models import Author
authors = Author.objects.annotate(num_articles=Count('articles')).order_by('-num_articles')
Then each item in the authors
QuerySet has a num_articles
property.
I usually set a related_name
but this seems less common in the Wagtail docs, and I almost hadn't noticed I'd copied that behaviour here, so this threw me.
If anyone's reading this and can give examples of why/when you'd want to set related_name
to '+'
, I'd be interested to know. Thanks.
add a comment |
Coming back to this a day later, I think the only issue is having set related_name
on ArticleAuthorRelationship
's author
property to '+'
(which means there's no backwards relationship from Author
to this model).
If I instead do:
class ArticleAuthorRelationship(Orderable, models.Model):
author = models.ForeignKey(
'Author',
on_delete=models.CASCADE,
related_name='articles') # Changed this to 'articles'
# etc.
Then I can do this quite easily:
from django.db.models import Count
from my app.models import Author
authors = Author.objects.annotate(num_articles=Count('articles')).order_by('-num_articles')
Then each item in the authors
QuerySet has a num_articles
property.
I usually set a related_name
but this seems less common in the Wagtail docs, and I almost hadn't noticed I'd copied that behaviour here, so this threw me.
If anyone's reading this and can give examples of why/when you'd want to set related_name
to '+'
, I'd be interested to know. Thanks.
add a comment |
Coming back to this a day later, I think the only issue is having set related_name
on ArticleAuthorRelationship
's author
property to '+'
(which means there's no backwards relationship from Author
to this model).
If I instead do:
class ArticleAuthorRelationship(Orderable, models.Model):
author = models.ForeignKey(
'Author',
on_delete=models.CASCADE,
related_name='articles') # Changed this to 'articles'
# etc.
Then I can do this quite easily:
from django.db.models import Count
from my app.models import Author
authors = Author.objects.annotate(num_articles=Count('articles')).order_by('-num_articles')
Then each item in the authors
QuerySet has a num_articles
property.
I usually set a related_name
but this seems less common in the Wagtail docs, and I almost hadn't noticed I'd copied that behaviour here, so this threw me.
If anyone's reading this and can give examples of why/when you'd want to set related_name
to '+'
, I'd be interested to know. Thanks.
Coming back to this a day later, I think the only issue is having set related_name
on ArticleAuthorRelationship
's author
property to '+'
(which means there's no backwards relationship from Author
to this model).
If I instead do:
class ArticleAuthorRelationship(Orderable, models.Model):
author = models.ForeignKey(
'Author',
on_delete=models.CASCADE,
related_name='articles') # Changed this to 'articles'
# etc.
Then I can do this quite easily:
from django.db.models import Count
from my app.models import Author
authors = Author.objects.annotate(num_articles=Count('articles')).order_by('-num_articles')
Then each item in the authors
QuerySet has a num_articles
property.
I usually set a related_name
but this seems less common in the Wagtail docs, and I almost hadn't noticed I'd copied that behaviour here, so this threw me.
If anyone's reading this and can give examples of why/when you'd want to set related_name
to '+'
, I'd be interested to know. Thanks.
answered Nov 21 at 13:37
Phil Gyford
3,99653174
3,99653174
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53398063%2fwagtail-getting-a-list-of-snippets-with-counts-of-related-parents%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