[Python] (fields.E331) Field specifies a many-to-many relation through model, which has not...

Discussão em 'Python' iniciado por Stack, Março 29, 2025.

  1. Stack

    Stack Membro Participativo

    When I run makemigrations I get the error


    teams.Team.members: (fields.E331) Field specifies a many-to-many relation through model 'TeamMember', which has not been installed.
    ​

    from django.db import models
    from django.conf import settings

    from common import TimestampMixin
    from users.models import User


    class Team(models.Model, TimestampMixin):
    name = models.CharField(max_length=100)
    owner = models.ForeignKey(
    User,
    related_name='owned_teams',
    on_delete=models.CASCADE
    )
    members = models.ManyToManyField(
    User,
    through='TeamMember',
    related_name='teams'
    )

    def __str__(self):
    return self.name


    class TeamMember(models.Model, TimestampMixin):
    user = models.ForeignKey(
    User,
    on_delete=models.CASCADE
    )
    team = models.ForeignKey(
    Team,
    on_delete=models.CASCADE
    )

    def __str__(self):
    return f"{self.user} in {self.team}"


    I don't get why it's happening because the 'teams' app is installed and both Team and TeamMember is in the same file. Any ideas?

    Continue reading...

Compartilhe esta Página