2

UPDATE:

For clarification sake, I feel like I need to go through the Placement model to get the results I need, because I need the order field from that model.

I am trying to get all article objects that are part of a section in an edition that is in an issue. I am stumped, even after looking at the documentation for django 2.x

I can get all editions with all of their sections that are part of an issue, but I cannot figure out how to get articles that are related to a section per edition.

For context, here is my working query inside a django view without getting articles:

def issue_edit_form(request, iid=0):
    latest = utils.get_latest()
    issue = Issue.objects.get(pk=iid)
    editions = issue.edition_set.all()
    sections = []
    for i in editions:
        sections.append(i.section_set.all().order_by("order"))
    return render(request, 'issue_editor.html', {'latest': latest, 'issue': issue, 'editions': editions,
                                                 'sections': sections, 'article_form':
                                                     ArticleForm})

Here is my models.py

class Issue(models.Model):
        volume = models.SmallIntegerField(default=0)
        issue = models.SmallIntegerField(default=0)
        issue_date = models.DateField()
        published = models.BooleanField(default=False)

        class Meta:
            verbose_name = "   Issue"

        def __str__(self):
            return "Vol. " + str(self.volume) + " Issue: " + str(self.issue) + ": " + str(self.issue_date)


class Edition(models.Model):
        edition = models.CharField(max_length=100)
        issue = models.ForeignKey(Issue, on_delete=models.CASCADE, null=True)

        class Meta:
            verbose_name = "  Edition"

        @property
        def issue__name(self):
            return self.issue.issue_date


        def __str__(self):
            return self.edition


class Section(models.Model):
        section_name = models.CharField(max_length=100)
        section_image = models.ImageField(blank=True, null=True)
        order = models.SmallIntegerField(default=1)
        section_hidden = models.BooleanField(default=False);
        edition = models.ForeignKey(Edition, on_delete=models.CASCADE, null=True)

        class Meta:
            verbose_name = " Section"

        def __str__(self):
            return self.section_name


class Article(models.Model):
        title = models.CharField(max_length=255)
        article_url = models.URLField()
        admin_description = models.CharField(max_length=255, blank=True)
        # article_order = models.SmallIntegerField(default=1)
        sections = models.ManyToManyField(Section, through="Placement")

        class Meta:
            verbose_name = "Article"

        def __str__(self):
            return self.title


class Placement(models.Model):
        article = models.ForeignKey(Article, on_delete=models.DO_NOTHING, null=True)
        section = models.ForeignKey(Section, on_delete=models.DO_NOTHING, null=True)
        order = models.SmallIntegerField(default=1)
ja408
  • 798
  • 5
  • 16
  • `Article.objects.filter(section__edition__issue_id=issue_id)` will give you article objects that are part of a section in an edition that is in an issue. Do you want to group it further by editions? – Nihal Sharma Feb 22 '18 at 08:08
  • Thank you Nihal! I would like the output to be in a template where I can loop through articles and put them in their section, and have those sections in their editions. Does that make sense? Here is a screenshot that represents this. https://pasteboard.co/H8N2zTt.png "Regular", "Parents", etc are the editions. "AWARDS...", "ALUMNI", etc are the sections. – ja408 Feb 22 '18 at 08:21
  • Hi Nihal, This gives me the article objects, but I need to go through the Placement model to get the order for the section an article is in. Do you know how I would go through Placement to get the results I'm looking for? – ja408 Feb 22 '18 at 18:50

1 Answers1

1

So if you want a list of articles for each section you can do this

edition_sections = edition.section_set.all()
for section in edition_sections:
    articles = section.articles_set.all()

Or if you don't need to process the article sets individually this should also work

edition_sections = edition.section_set.all()
articles = Article.objects.filter(sections__in=edition_sections)

There are some good examples in this part of the docs https://docs.djangoproject.com/en/2.0/topics/db/models/#intermediary-manytomany

hwhite4
  • 695
  • 4
  • 6
  • this doesn't seem to work. I need to get at the "order" field that is in the Placement model. The Placement model has the section an article belongs to including its order within that section. – ja408 Feb 22 '18 at 18:49
  • Alright. If articles are only within sections and not the other way around you could replace ManyToManyField with just a ForeignField. But anyways once you have the article are you getting the order with article.placement.order? – hwhite4 Feb 23 '18 at 06:00