1

在我的 Django 应用程序中,我有以下 models.py:

from django.db import models
import datetime

class Job(models.Model):
    name = models.CharField(max_length = 250, null = False)
    user = models.CharField(max_length = 30, null = False)
    command = models.CharField(max_length = 1000, null = False)
    whenToRun = models.DateTimeField('Run Date', null = False)
    output = models.CharField(max_length = 100000, null = True)

class Host(models.Model):
    jobs = models.ManyToManyField(Job, blank = True)
    name = models.CharField(max_length = 100, null = False)
    hasRun = models.BooleanField(default = False)

我在https://docs.djangoproject.com/en/dev/topics/db/examples/many_to_many/的指导下在 Host 类中添加了作业分配。我能够毫无问题地添加主机(从管理页面),但是当我尝试添加作业时,出现以下错误:

Exception at /admin/Minion/job/add
<class 'Minion.models.Host'> has no ForeignKey to <class 'Minion.models.Job'>

我也尝试向 Job 类添加 ManyToManyField 分配,但它告诉我名称 Host 未定义。有谁知道我可以做些什么来使这个领域正常运作?提前感谢您的帮助。

4

1 回答 1

1

你需要声明inlinesfor ManyToManyinadmin.py

检查文档以了解如何声明内联

像这样的东西:

class JobInline(admin.TabularInline):
     model = Host.jobs.through
于 2013-06-24T14:26:14.440 回答