Using Django "through" argument for saving additional information in ManyToMany field

I am trying to create a scenario where I may add an "order" of level (to add the degree of escalation) for the saved instances of a model "ContactPerson" according to some criteria (say seniority or position of responsibility). The form is intended to look someting like this:

Contact Person Level
Franc Correa 2
Liz Burnett 3
Christian Amarilo 3
Mark Tannit

The Level values will be entered at runtime to the (already) created contact persons. (The empty field for the last entry in the above table signifies that a level is not yet assigned.)

I have created the following models:

class ContactPerson(models.Model):
    name = models.CharField(max_length=128)
    designation = models.CharField(max_length=128)

class Level(models.Model):
    level = models.PositiveSmallIntegerField()
    desig_person = models.ManyToManyField(ContactPerson, through='Order')

class Order(models.Model):
    contact_person = models.ForeignKey(ContactPerson, on_delete=models.CASCADE)
    level_order = models.ForeignKey(Level, on_delete=models.CASCADE)
    location = ...

Now I create / add persons (in model ContactPerson) and later want to assign "level/degree of escalation" in model "Order" in field level_order at runtime. To that end I am using the following model form and assigning to an inlineformset_factory:

class AssignEscalationLevelForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(AssignEscalationLevelForm, self).__init__(*args, **kwargs)
        self.auto_id = True

    class Meta:
        model = Order
        fields = ('contact_person', 'level_order', 'location')

As already stated above, I want to have the field level_order placed along side all the saved values of the field contact_person in the same row. However, what I am getting is all the contact_person as a drop down list and a blank dropdown field (for entering numeric order) for level_order along side it in the same row. What I want to do is to display all values of Contact Person in successive rows and the field for level_order (as shown in the table at the top).

Can someone guide me as to where I am going wrong. This the first time I am using Django's through argument and I am not very confident if I got it right.


Note: The header level model has not been shown here.

Back to Top