1

I have something like this in my models.py:

class Section(models.Model):
    title = models.CharField(max_length=50)

class Lesson1(models.Model):
    section = models.ForeignKey(Section)
    title = models.CharField(max_length=50)
    ...
    sort_index = models.IntegerField()

class Lesson2(models.Model):
    section = models.ForeignKey(Section)
    title = models.CharField(max_length=50)
    ....
    sort_index = models.IntegerField()

And I need to display them mixed, like this:

section title:
-lesson1 title
-lesson2 title
-lesson1 title

next section title:
-lesson2 title
-lesson2 title
-lesson1 title

Is there a nice way to do this?

SaulTigh
  • 913
  • 2
  • 14
  • 27
  • what determines the order of lessons? is it the `sort_index`? If so what happens if the lesson1 and lesson2 for a particular section have the same `sort_index` value, who gets precendence? – dm03514 Feb 19 '14 at 15:02
  • @dm03514, it does not matter, it may be additional ordering by title or something – SaulTigh Feb 19 '14 at 15:07
  • 1
    Are you sure you need to Lesson models with the same structure? Maybe explain why you need them as there might be more elegant solutions to this... – Bernhard Vallant Feb 19 '14 at 15:07
  • @BernhardVallant, I've 3 types of lessons: text, video and test lessons, they only have foreign key to Section, title and sort_index fields in common – SaulTigh Feb 19 '14 at 15:17
  • If you can change that I would maybe rather go for a `Lesson` base model and have the other models inherit from this. You could use something like https://django-polymorphic.readthedocs.org/en/latest/ to implement the polymorphic behaviour of the lessons... The other possibilty would be using a generic foreign key... – Bernhard Vallant Feb 19 '14 at 15:21
  • @BernhardVallant, thank you, I'll try this app, but I'll also live the question opened as it be nice to get this done with django default possibilities – SaulTigh Feb 19 '14 at 15:26
  • `sessions = Session.objects.all()` after getting all sessions, get all Lession1 and Lession2 as well `[(s.lession1_set, s.lession2_set) for s in sessions]`. Then sort every pair of (lession1, lession2) for every session. I guess I am not sure how to sort them in your case. by sort_index? – NullSpace Feb 19 '14 at 15:44
  • And the comment I posted above can simply be a method in Session class – NullSpace Feb 19 '14 at 15:49

0 Answers0