jpwatts / django-positions

A Django field for custom model ordering.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Multi-table inheritance

danxshap opened this issue · comments

I have models like this:

class SubUnit(models.Model):
   # .... some fields

class Task(models.Model):
    """
    Base class for lessons/exercises - ordered items within a sub-unit
    """
    sub_unit = models.ForeignKey(SubUnit)
    position = PositionField(collection='sub_unit')

class Lesson(Task):
    # ... lesson-specific fields

class Exercise(Task):
    # ... exercise-specific fields

Basically a SubUnit contains many Task's, each of which may be a Lesson or an Exercise. I'd like to keep track of the position of each Task regardless of whether it's a Lesson or an Exercise. If I add a Lesson, it has position 0, and then if I add an Exercise to the same SubUnit I expect/want it to have position 1, but it instead has position 0 - the Lesson position counter is totally separate from the Exercise position counter.

Do you know of any workarounds for this?

Thank you!

Could you take a look at 1b84890 and see if it does what you need? It seems like there are legitimate reasons to want child models to be ordered independently as well as reasons to want them all to be part of the same sequence, so I added an argument to make it configurable. I stuck with independent ordering as the default, mostly to preserve backwards compatibility.

Thanks for the quick response!

Hmmm, seems like I'm running into some unexpected behavior. I also can't figure out how to run the tests...if I do python manage.py test positions I get ImproperlyConfigured: App with label positions is missing a models.py module.

Your commit seems to have fixed the scenario in which I'm appending lessons/exercises (i.e. creating them without specifying position), but it looks like even before the commit there was an issue that I didn't notice at first.

If I create Lesson (0), Lesson (1), Lesson (2), and then I do Lesson.objects.create(sub_unit=same_sub_unit, name='whatever', position = 0), it's not updating the existing 0/1/2 to 1/2/3 like it does for simple models that don't use multi-table inheritance...

Happy to play around with it further and write up failing test cases once I can actually run the tests - any ideas there?

Thanks again!

I run the tests like this:

django-admin.py test --settings=positions.examples.settings

Thanks for being willing to dig in a bit. In the spirit of full disclosure, I should probably tell you that I wrote this code a long time ago and I'm not using it in any current projects, so it doesn't exactly have my full attention. That said, I've tried to keep it working on current versions of Django and to fix bugs as they're identified. If you've got the interest and energy, pull requests are absolutely welcome.

just curious @jpwatts has this been fixed?

Sorry; that was sloppy of me. I was trying to clean up a bit around here and wasn't seeing your test fail, but I didn't catch that Django 1.6 wasn't running the doctests. That's fixed now, but this issue is still open.

Good stuff, thanks.