Wagtail: Getting a list of snippets with counts of related parents












0














Using Wagtail I have ArticlePages, each of which can have 0 or more Authors, which are Snippets.



I'd like to get a list of Authors with the number of ArticlePages 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!










share|improve this question



























    0














    Using Wagtail I have ArticlePages, each of which can have 0 or more Authors, which are Snippets.



    I'd like to get a list of Authors with the number of ArticlePages 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!










    share|improve this question

























      0












      0








      0







      Using Wagtail I have ArticlePages, each of which can have 0 or more Authors, which are Snippets.



      I'd like to get a list of Authors with the number of ArticlePages 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!










      share|improve this question













      Using Wagtail I have ArticlePages, each of which can have 0 or more Authors, which are Snippets.



      I'd like to get a list of Authors with the number of ArticlePages 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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 at 17:08









      Phil Gyford

      3,99653174




      3,99653174
























          1 Answer
          1






          active

          oldest

          votes


















          0














          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.






          share|improve this answer





















            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%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









            0














            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.






            share|improve this answer


























              0














              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.






              share|improve this answer
























                0












                0








                0






                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.






                share|improve this answer












                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 at 13:37









                Phil Gyford

                3,99653174




                3,99653174






























                    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.





                    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.




                    draft saved


                    draft discarded














                    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





















































                    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

                    To store a contact into the json file from server.js file using a class in NodeJS

                    Redirect URL with Chrome Remote Debugging Android Devices

                    Dieringhausen