Python Forum

Full Version: Python Database Django
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a list of questions with a list of multiple choice answers.

I am trying to get details of a user who will then answer the questions and store their answers next to their name. The questions/answers may change moving forward.

I have the following code is this correct can you help please?


from django.db import models

class Investor(models.Model):
    Investor = models.CharField(max_length=100, default="")

    def __str__(self):
        return self.Investor

class Question(models.Model):
    Question_text = models.TextField(max_length=200,default="")

    def __str__(self):
        return self.Question_text

class Option(models.Model):
    Question = models.ForeignKey(Question, on_delete=models.CASCADE)
    Option = models.CharField(max_length=50, default="")

    def __str__(self):
        return self.Option

class Answer(models.Model):
    Investor = models.ForeignKey(Investor, on_delete=models.CASCADE, default="")
    Question = models.OneToOneField(
        Question,
        on_delete=models.CASCADE,
        primary_key=True,
    )
    Answer = models.OneToOneField(
        Option,
        on_delete=models.CASCADE,
        primary_key=True,
    )
I am trying to get the following to work.

Essentially I want to load a number of investors in (users). I then want to load a number of questions in. I want each Investor to answer the questions which are all True/False. The below is what I have on this topic.

Giles

from django.db import models

class Investor(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)

    def __str__(self):
            return self.first_name
            return self.last_name

class Question (models.Model):
    question_text = models.CharField(max_length=100)

    def __str__(self):
        return self.question_text

class Option (Investor):
    Question = models.ForeignKey(Question, on_delete=models.CASCADE)
    Option = models.CharField(max_length=100)
    Answer = models.BooleanField(default=False)
I am trying to get the following to work.

Essentially I want to load a number of investors in ("Investors"). I then want to load a number of questions in. I want each Investor to answer the questions which are all True/False. The below is what I have on this topic. Any help is appreciated this is sending me mad.

Giles

from django.db import models

class Investor(models.Model):
    name = models.CharField(max_length=50)

    def __str__(self):
            return self.first_name

class Question (models.Model):
    question_text = models.CharField(max_length=100)


    def __str__(self):
        return self.question_text

class Option (models.Model):
    Question = models.ForeignKey(Question, on_delete=models.CASCADE)
    Option = models.CharField(max_length=100)
    Answer = models.BooleanField(default=False)
How about a Response table, that has a foreign key to both an Investor and an Question, along with a foreign key to which Option they chose?
I've merged your posts together. Please, don't post the same thing multiple times.
Thanks so much this is now working how I wanted it to.

I sincerely apologise about the multiple posts thanks for merging them for me. I am new to this so just learning. I will also try harder to get the Python Code in the correct font.

I am now trying to open up the home page and then click a button to get to the investor page where they will put their details in. I am looking in the views.py file see below does this look correct as I can't get the home page to show when I run the server.

def home(request):
    return render(request,"home.html")

def create(request):
    if request.method == 'POST':
        name =  request.POST.get("name")
    return render(request,"create.html")