Python Forum
Python Database Django - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Python Database Django (/thread-8532.html)



Multiple choice questions with answers linked to users - gilesgerman - Feb-22-2018

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



Python Database Django - gilesgerman - Feb-24-2018

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)



Python Code for Identifying database relationships - gilesgerman - Feb-24-2018

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)



RE: Python Code for Identifying database relationships - nilamo - Feb-24-2018

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?


RE: Python Database Django - nilamo - Feb-24-2018

I've merged your posts together. Please, don't post the same thing multiple times.


RE: Python Database Django - gilesgerman - Feb-24-2018

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