[Django]-Django modify bridge table

3👍

This can be done by creating a “through” table and adding the fields on it. Check out the docs at https://docs.djangoproject.com/es/1.10/topics/db/models/

class Group(models.Model):
    name = models.CharField(max_length=128)
    members = models.ManyToManyField(Person, through='Membership')

    def __str__(self):              # __unicode__ on Python 2
        return self.name

class Membership(models.Model):
    person = models.ForeignKey(Person, on_delete=models.CASCADE)
    group = models.ForeignKey(Group, on_delete=models.CASCADE)
    date_joined = models.DateField()
    invite_reason = models.CharField(max_length=64)

1👍

This bridge table is called association table. Imo it is bad practice to edit this table that way. Connecting models by many to many field suggest that there is no model between. If your association table is supposed to represent additional data then you should create new model

class NewModel(models.Model):
    attender = models.ForeignKey(User, null=False)
    event = models.ForeignKey(Event, null=False)
    # additional fileds

and remove many to many relationship from event

👤pato

Leave a comment