shuttle87
shuttle87

Reputation: 15934

Django ordered sequence model design

I'm currently creating a database of techniques for a particular sport. In this particular sport there is a set of unique component movements (Component) and these combine together to make different moves (Move).

The order in which the components happens matters. For example we might have 3 components A, B and C then the move defined by A -> B -> C is distinct from A -> C -> B. As you can see there are many more moves than components. At some point in time it will be important to be able to search for Moves based on which Components are present in the different Moves.

So I initially was thinking that I would make a class Component and a class Move with a skeleton structure similar to:

class Component(models.Model):
    """Defines a unique component movement"""
    name = model.CharField(max_length=30)
    def __unicode__(self):
        return self.name

class Move(models.Model):
    """Define a move as a sequence of Components in the order in which they happen"""
    name = model.CharField(max_length=40)
    component_sequence = #what should I do here?
    def __unicode__(self):
        return self.name 

Now my question is what is the best way to implement component_sequence in a Django model? Initially I was thinking that I might be able to somehow store a csv list of the primary keys for the Component objects but this seems nasty so I was wondering if there's some better way of doing this?

Upvotes: 3

Views: 240

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798744

Create a M2M model that connects Move to Component, and add an ordinal field. Create a unique index across all three.

Upvotes: 2

Related Questions