Django makemigrations 关于多对多关系的错误

Django makemigrations error on many to many relationship

我想要的:

存储关于 运行 组人员的信息。

我做了什么:

from django.db import models
from django.contrib.auth.models import User
from datetime import timedelta


class Route(models.Model):
    name = models.CharField(max_length=50)


class Run(models.Model):
    date = models.DateField()
    type = models.ForeignKey(Route, on_delete=models.PROTECT)
    runners = models.ManyToManyField(User, through='RunnerResult', through_fields=["user", "run"])


class RunnerResult(models.Model):
    user = models.ForeignKey(User, on_delete=models.PROTECT)
    run = models.ForeignKey('Run', on_delete=models.PROTECT)
    result = models.DurationField(default=timedelta())

问题:

当我执行 makemigrations 时出现以下错误:

SystemCheckError: System check identified some issues:

ERRORS:
run.Run.runners: (fields.E339) 'RunnerResult.run' is not a foreign key to 'User'.
        HINT: Did you mean one of the following foreign keys to 'User': user?
run.Run.runners: (fields.E339) 'RunnerResult.user' is not a foreign key to 'Run'.
        HINT: Did you mean one of the following foreign keys to 'Run': run?

试图交换 through_fields 和模型之间的彼此和其他一些动作。我开始反思自己对M2M关系的误解了。

您在错误的顺序中指定了through_fields。您首先应该指定引用 source 的关系,然后指定指向 target 的关系,因此:

class Run(models.Model):
    date = models.DateField()
    type = models.ForeignKey(Route, on_delete=models.PROTECT)
    runners = models.ManyToManyField(
        User,
        through='RunnerResult',
        <b>through_fields=('run', 'user')</b>
    )

因为只有一个 ForeignKeyRun,和一个到 User,所以你不需要指定 through_fields=… parameter [Django-doc]。所以你可以将其实现为:

class Run(models.Model):
    date = models.DateField()
    type = models.ForeignKey(Route, on_delete=models.PROTECT)
    runners = models.ManyToManyField(
        User,
        through='RunnerResult'
        # <i>no</i> through_fields
    )

Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.