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)