How to retrieve a polymorphic model from a single table in django - or how to implement polymorphic behaviour in django
Can I read polymorphic models from a single database table, with their behaviour depending on a (boolean) field of the model? 

In one of my models the behaviour is slightly different if the instance is 'forward' vs. 'backward' or 'left' vs. 'right'. That leads to a lot of if-clauses and code duplication. So I want to have a Forward- and a Backward-variant of the model that encapsulate the different behaviours. 

But how can I make the models manager return the instances of the right classes? Do I have to overwrite `__init__` of the model?

Maybe it's easier to explain with an example. What I'm doing:

    class Foo(models.Model):
        forward = models.BooleanField()
        other_fields = ...

        def do_foobar(bar):
            if self.forward:
                gap = bar.end_pos - bar.current_pos
                self.do_forward_move(max = gap)
                if self.pos==bar.end_pos:
                    and so on ...
            else:
                gap = bar.current_pos - bar.start_pos
                self.do_backward_move(max = gap)
                if self.pos==bar.start_pos:
                    and so on ...

What I want to do:


    class Foo(models.Model):
        forward = models.BooleanField()
        other_fields = ...

        def __init__(*args, **kwargs):
            """ return ForwardFoo or BackwardFoo 
            depending on the value of 'forward'"""
            How?

        def do_foobar(bar):
            gap = self.calculate_gap(bar)
            self.do_move(max = gap)
            if self.end_point_reached():
                and so on ...

    class ForwardFoo(Foo):
        def calculate_gap(bar):
            return bar.end_pos - bar.current_pos
    and so on ...

    for f in Foo.objects.all():
        f.do_foobar(bar)

Or is there a totally different way to avoid this kind of code duplication?
How to retrieve a polymorphic model from a single table in django - or how to implement polymorphic behaviour in django
===

Can I read polymorphic models from a single database table, with their behaviour depending on a (boolean) field of the model? 

In one of my models the behaviour is slightly different if the instance is 'forward' vs. 'backward' or 'left' vs. 'right'. That leads to a lot of if-clauses and code duplication. So I want to have a Forward- and a Backward-variant of the model that encapsulate the different behaviours. 

But how can I make the models manager return the instances of the right classes? Do I have to overwrite `__init__` of the model?

Maybe it's easier to explain with an example. What I'm doing:

    class Foo(models.Model):
        forward = models.BooleanField()
        other_fields = ...

        def do_foobar(bar):
            if self.forward:
                gap = bar.end_pos - bar.current_pos
                self.do_forward_move(max = gap)
                if self.pos==bar.end_pos:
                    and so on ...
            else:
                gap = bar.current_pos - bar.start_pos
                self.do_backward_move(max = gap)
                if self.pos==bar.start_pos:
                    and so on ...

What I want to do:


    class Foo(models.Model):
        forward = models.BooleanField()
        other_fields = ...

        def __init__(*args, **kwargs):
            """ return ForwardFoo or BackwardFoo 
            depending on the value of 'forward'"""
            How?

        def do_foobar(bar):
            gap = self.calculate_gap(bar)
            self.do_move(max = gap)
            if self.end_point_reached():
                and so on ...

    class ForwardFoo(Foo):
        def calculate_gap(bar):
            return bar.end_pos - bar.current_pos
    and so on ...

    for f in Foo.objects.all():
        f.do_foobar(bar)

Or is there a totally different way to avoid this kind of code duplication?