在我的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'>
我尝试将ManyToManyField赋值添加到Job类,但它告诉我名称Host未定义。有谁知道我能做些什么来使这个领域正常运作?提前感谢您的帮助。
答案 0 :(得分:1)
您需要在inlines
ManyToMany
声明admin.py
检查the documentation如何声明内联
这样的事情:
class JobInline(admin.TabularInline):
model = Host.jobs.through