How to have a Django relational database with four tables?

749 views Asked by At

I am creating a website using Django and have a problem with the database! I have four tables (topics, questions, answers, and images). Each one of these tables has an id column and I would like to connect these four tables together.

I have tried to use a ForeignKey() but that didn't work out. I am receiving an error message. I don't know if I can use ManyToManyField() in this case, because it is only one column that I am trying to connect.

This is the code:

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()

This is the code with the 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)

And this is the error message that I am receiving:

topic_question = models.ForeignKey(questions, on_delete=CASCADE) NameError: name 'questions' is not defined

1

There are 1 answers

10
Raydel Miranda On BEST ANSWER

At the point you're trying to use the question class name for indicating the related model, such class is not defined, as the error states. When you're referencing a model that is defined later in the code you must enclose the name in "":

topic_question = models.ForeignKey("questions", on_delete=CASCADE)

Here is the related docs: https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey