M2M relation to itself through another model // 1-minute guide 🐍↩️

Say, you want your Tag model to contain a subtags field that has Tag instances. And you need to make a through table.


class Tag(models.Model)
   name = models.Charfield(max_length=100)
   subtags = models.ManyToManyField('self',
                                    # through_fields='tag',

class ToSubTag(models.Model):
    tag = models.ForeignKey(Tag,
    subtag = models.ForeignKey(Tag,
    level = models.PositiveIntegerField()

Breaking it down:

  • 'self' is used to establish a relation to the same model.
  • through='ToSubTag' is used to establish a table with additional information about this Tag to Subtag relation. In my example, it is level = models.PositiveIntegerField().
  • related_name = 'main_tag' is used to refer to the main Tag model from a sub Tag model. In the ToSubTag model they are mandatory for both relations to Tag, because if there are two of them.
  • through_fields = 'tag' is used to let Django know which ToSubTag field is assigned to the main Tag model (because ToSubTag has two ForeignKey relations to Tag – tag and subtag); BUT when I use it and make an Inline in admin.py, I get an error: The intermediary model 'testapp.ToSubTag' has no field 'a'. That’s why it is commented. Instead of it, I use fk_name property in the inline (see it below).
  • symmetrical = False is used because this particular relation has a strict hierarchy: Tag -> Subtag. Otherwise if you added a subTag to mainTag.subtags, then this mainTag would be added to subTag.subtags. This property has a good explanation in the docs – if you add a secondPerson to firstPerson.friends, then the firstPerson would be added to the secondPerson.friends, because they can only be friends mutually.


class SubTagsToTag(admin.TabularInline):
    model = Tag.subtags.through
    fk_name = "tag"

class TagAdmin(admin.ModelAdmin):
    inlines = [

Everything here is pretty standard, except for:

  • fk_name = "tag" is basically the same as through_fields = 'tag'.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.