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?

8
  • 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? Commented Feb 19, 2014 at 15:02
  • @dm03514, it does not matter, it may be additional ordering by title or something Commented Feb 19, 2014 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... Commented Feb 19, 2014 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 Commented Feb 19, 2014 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 django-polymorphic.readthedocs.org/en/latest to implement the polymorphic behaviour of the lessons... The other possibilty would be using a generic foreign key... Commented Feb 19, 2014 at 15:21

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.