我想在django中建立一个多人关系
models.py:
class student(models.Model):
id = models.AutoField(primary_key=True)
First_Name = models.CharField(max_length=100,null=True)
Last_Name = models.CharField(max_length=100,null=True)
Birthday = models.CharField(max_length=10,null=True)
Phone = models.CharField(max_length=20,null=True)
Mobile = models.CharField(max_length=20,null=True)
STNO = models.CharField(max_length=10,null=True)
Father_Name = models.CharField(max_length=100,null=True)
Father_Job = models.CharField(max_length=100,null=True)
Father_Phone = models.CharField(max_length=20,null=True)
ID_Code = models.CharField(max_length=10,null=True)
National_ID = models.CharField(max_length=10,null=True)
Address = models.CharField(max_length=200,null=True)
Study_Field = models.CharField(max_length=100,null=True)
Probation = models.BooleanField(default=False)
def __STR__ (self):
return self.STNO
class Meta:
ordering = ('id','First_Name','Last_Name','STNO','ID_Code','National_ID',
'Phone','Mobile','Father_Name','Father_Phone','Father_Job',
'Birthday','Address','Study_Field','Probation')
class lesson(models.Model):
id = models.AutoField(primary_key=True)
Name = models.CharField(max_length=100,null=True)
Code = models.CharField(max_length=100,null=True)
Capacity = models.IntegerField(null=True)
UNO = models.IntegerField(null=True)
CNO = models.IntegerField(null=True)
Hour = models.IntegerField(null=True)
Student = models.ManyToManyField(student)
def __STR__ (self):
return self.Code
class Meta:
ordering = ('id','Name','Code','Capacity','UNO',
'CNO','Hour')
admin.py:
class lesson_admin(admin.ModelAdmin):
fieldsets = [
('Lesson Info : ',{'fields':['Name','Code','Capacity']}),
('ManyToMany : ',{'fields':['Student']})
]
sqlmigrate
BEGIN;
CREATE TABLE "school_manager_lesson_Student" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "lesson_id" integer NOT NULL REFERENCES "school_manager_lesson" ("id"), "student_id" integer NOT NULL REFERENCES "school_manager_student" ("id"),UNIQUE ("lesson_id", "student_id"));
INSERT INTO "school_manager_lesson_Student" (id, lesson_id, student_id) SELECT id, lesson_id, student_id FROM "school_manager_lesson_student";
DROP TABLE "school_manager_lesson_student";
CREATE INDEX "school_manager_lesson_Student_55174b7b" ON "school_manager_lesson_Student" ("lesson_id");
CREATE INDEX "school_manager_lesson_Student_30a811f6" ON "school_manager_lesson_Student" ("student_id");
COMMIT;
在迁移中,表创建但是当我想使用软件时我有这个错误: “没有这样的表:school_manager_lesson_Student”
Django文档中的刚才说要定义多对多关系,请使用ManyToManyField
答案 0 :(得分:0)
检查您的应用是否包含在设置文件中已安装应用的列表中。然后检查您的迁移是否已应用。可以帮助您的命令: