我忽略了一些东西:TypeError:create()得到意外的关键字参数'system'

时间:2018-03-13 15:01:58

标签: python django python-3.x

这可能是一个noob错误,但我找不到错误。

以下是project.models

class ProjectManager(models.Manager):
    """"""
    def create(self, owner, project_name):
        project = super().create(owner=owner, project_name=project_name)
        project.save()

        System.objects.create(project=project, system_name="System initial name")

        return project

class Project(models.Model):
    objects = ProjectManager()

    owner           = models.ForeignKey('auth.User', related_name='projects', on_delete=models.CASCADE)
    project_name    = models.CharField(max_length=200)

在system.models中,我现在定义了系统和电路模型,即每个项目至少有一个系统,每个系统至少有一个电路。

class SystemManager(models.Manager):
    """"""
    def create(self, project, system_name):
        system = super().create(project=project, system_name=system_name)
        system.save()

        Circuit.objects.create(system=system, circuit_name="Primary circuit")
        return system

class System(models.Model):
    objects = SystemManager()

    project     = models.ForeignKey('solgeo.Project', related_name='systems', on_delete=models.CASCADE)
    system_name = models.CharField(max_length=200)

    @classmethod
    def create(cls, project):
        system = cls(project=project)

    def __str__(self):
        return "system name: " + str(self.system_name)

class CircuitManager(models.Manager):
    """"""
    def create(self, project, system_name):
        circuit = super().create(system=system, circuit_name=circuit_name)
        circuit.save()
        return circuit


class Circuit(models.Model):
    """Models a hydraulic circuit"""
    objects = CircuitManager()

    system = models.ForeignKey(System, related_name='circuits', on_delete=models.CASCADE)
    circuit_name = models.CharField(max_length=200)

现在我收到错误:create()得到了一个意外的关键字参数'system'。当我注释掉:Circuit.objects.create(system = system,circuit_name =“Primary circuit”)然后错误消失。

1 个答案:

答案 0 :(得分:0)

create的{​​{1}}方法CircuitManager作为参数而不是system_name。将其更改为system,因为您也在方法体中使用它。