我正在使用Django创建一个网站,并且数据库存在问题!我有四个表(主题,问题,答案和图像)。这些表中的每一个都有一个id列,我想将这四个表连接在一起。
我试过使用ForeignKey()但是没有用。我收到一条错误消息。我不知道我是否可以ManyToManyField()在这种情况下使用,因为我只想连接一列。
这是代码:
from django.db import models
# Create your models here.
class topics(models.Model):
topic_id = models.AutoField(primary_key=True)
topic_level = models.BooleanField()
topic_name = models.TextField()
class questions(models.Model):
question_id = models.AutoField(primary_key=True)
description = models.TextField()
questions_type = models.BooleanField()
class answers(models.Model):
answer_id = models.AutoField(primary_key=True)
description = models.TextField()
class images (models.Model):
image_id = models.AutoField(primary_key=True)
image_blob = models.BinaryField()
这是代码ForeignKey():
from django.db import models
# Create your models here.
class topics(models.Model):
topic_id = models.AutoField(primary_key=True)
topic_level = models.BooleanField()
topic_name = models.TextField()
topic_question = models.ForeignKey(questions, on_delete=CASCADE)
topic_answer = models.ForeignKey(answers, on_delete=CASCADE)
topic_image = models.ForeignKey(images, on_delete=CASCADE)
class questions(models.Model):
question_id = models.AutoField(primary_key=True)
description = models.TextField()
questions_type = models.BooleanField()
question_topic = models.ForeignKey(topics, on_delete=CASCADE)
question_answer = models.ForeignKey(answers, on_delete=CASCADE)
question_image = models.ForeignKey(images, on_delete=CASCADE)
class answers(models.Model):
answer_id = models.AutoField(primary_key=True)
description = models.TextField()
answer_topic = models.ForeignKey(topics, on_delete=CASCADE)
answer_question = models.ForeignKey(questions, on_delete=CASCADE)
answer_image = models.ForeignKey(images, on_delete=CASCADE)
class images (models.Model):
image_id = models.AutoField(primary_key=True)
image_blob = models.BinaryField()
image_topic = models.ForeignKey(topics, on_delete=CASCADE)
image_question = models.ForeignKey(questions, on_delete=CASCADE)
image_answer= models.ForeignKey(answers, on_delete=CASCADE)
这是我收到的错误消息:
topic_question = models.ForeignKey(questions,on_delete = CASCADE)NameError:名称'问题'未定义
在您尝试使用question类名来指示相关模型时,未定义此类,因为错误状态。当您引用稍后在代码中定义的模型时,必须将名称括在"":
topic_question = models.ForeignKey("questions", on_delete=CASCADE)
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。