Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Database Django
#1
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,
    )
Reply
#2
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)
Reply
#3
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)
Reply
#4
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?
Reply
#5
I've merged your posts together. Please, don't post the same thing multiple times.
Reply
#6
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")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Django: View is unable to find attributes of database model pythonpaul32 0 480 Dec-07-2023, 06:38 PM
Last Post: pythonpaul32
  Save JSON data to sqlite database on Django Quin 0 2,804 Mar-26-2022, 06:22 PM
Last Post: Quin
  Send email to gmail after user fill up contact form and getting django database updat Man_from_India 0 2,070 Jan-22-2020, 03:59 PM
Last Post: Man_from_India
  how retrieve database save multiple data in web Django 2.1 taomihiranga 0 2,763 Jul-30-2019, 04:58 PM
Last Post: taomihiranga
  Django: How to automatically substitute a variable in the admin page at Django 1.11? m0ntecr1st0 3 3,245 Jun-30-2019, 12:21 AM
Last Post: scidam
  Django- Remove leading zeros in values from database ntuttle 1 3,459 Mar-07-2019, 07:30 PM
Last Post: nilamo
  Django- which database i should use? with best speed and performance!! ma_norouzifar 4 4,776 Dec-10-2018, 09:47 AM
Last Post: ma_norouzifar
  How to save uploaded image url in database from Django? PrateekG 14 14,700 Jul-04-2018, 05:18 PM
Last Post: PrateekG
  Need your help to fix my database relationship in Django model PrateekG 0 2,631 Jul-02-2018, 11:08 AM
Last Post: PrateekG
  Why some Django fields are not saved in database? PrateekG 0 2,558 Jun-21-2018, 08:32 AM
Last Post: PrateekG

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020