[Answer]-Django Many-to-Many relionships

1👍

In your SQLAlchemy example you’ve explicitly set the backref to avoid a conflict: you need to do the same in the Django version using related_name.

class SubDomain(models.Model):
    sub_domain = models.ForeignKey(Domain)
    domain = models.ForeignKey(Domain, related_name='sub_domains_assocs')
    extra_data = models.CharField(max_length=127, blank=True)

This does seem a slightly strange model though; you might be better off with an explicit hierarchy provided by something like MPTT.

Leave a comment