0

I have the following two models:

class Step(models.Model):
    start_time = models.TimeField()
    time = models.IntegerField()
    schedule = models.ForeignKey(Schedule, on_delete=models.CASCADE)

class Schedule(models.Model):
    identifier = models.CharField(max_length=10)
    name = models.CharField(max_length=100)
    steps = models.ManyToManyField('manager.Step', related_name='steps')

However when editing the Schedule and adding steps via admin the Step does not update its schedule?

How do you handle two way binding like this in Django?

Titan Chase
  • 101
  • 1
  • 6
  • 1
    You can use use signals in django to deal with this kind of problem.https://docs.djangoproject.com/fr/4.0/ref/signals/#django.db.models.signals.m2m_changed – Thierno Amadou Sow Mar 16 '22 at 07:44
  • 1
    Why do you need to define the relationship in two places? If both are always going to be synchronised then it's sufficient just to have the foreign key - the many to many field is redundant. – solarissmoke Mar 16 '22 at 07:53
  • @solarissmoke I would like to be able to retrieve a `Schedule`'s steps when looking up a schedule – Titan Chase Mar 16 '22 at 12:09
  • You can do that without defining the relationship twice - how to do so is explained in the [documentation](https://docs.djangoproject.com/en/4.0/topics/db/queries/#following-relationships-backward). – solarissmoke Mar 16 '22 at 12:29

0 Answers0