Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Adding multiple models for the admin page in Django

Posted on 2015-01-23
9
Medium Priority
?
125 Views
Last Modified: 2015-02-07
Hello,

I'm trying to learn Django by building a quiz application.

This is what I have:
 - Table for Question
 - Table for Choices
 - Table for Quiz

I can link the Choices model with the Question model in the Admin page.

But when I try to link the Question model with the Quiz model in the Admin page, I can only add questions; but I cannot add choices. When I try to add the Choices model to the Quiz model without having a ForeignKey relationship between the Quiz and Choices table... but that makes no sense.

Is there any way I can associate the questions and choices in the Quiz page without having the relationship between Quiz and Choices?

Appreciate any help!
0
Comment
Question by:Errang Genevre
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40569156
>> Is there any way I can associate the questions and choices in the Quiz page without having the relationship between Quiz and Choices?

I don't think so, a quiz is made up out of x questions that each have y choices. So to create quiz #1 and then quiz #2 etc. you need to have a relation between quiz and questions (which quiz contains which questions).

For the questions, you need a relation to answers and they need to referred to a a foreign key because you don't want answers for question #1 to be linked to question #2. You can create different quiz's that are made up of a selection of questions. But answers always belong to a certain question.
0
 

Author Comment

by:Errang Genevre
ID: 40569484
So, I would need to have a relationship between Quiz, and Choices (answers) if I want to interact with Choices from the Quiz admin page?
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40569666
Are you following a tutorial? Got a link maybe?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Errang Genevre
ID: 40569706
I'm just following https://docs.djangoproject.com/en/1.7/intro/tutorial01/

I'm fairly new to Django; I tried a few combinations to get this working; basically variations of these two:
- adding the Choices table under QuizAdmin
- adding the choices under QuestionAdmin and adding QuestionAdmin to QuizAdmin

I tried searching Google, but I think I confused the poor thing by not phrasing the question properly; or the out of box Django stuff doesn't support this, and I need to write custom code for this.

I'd really appreciate an answer confirming either scenario, so I can focus on what's possible.
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40572140
I see, can you post your model? I just followed that tutorial, works fine so far. You have Quiz, the tutorial is about Poll, what did you change?
0
 

Author Comment

by:Errang Genevre
ID: 40572518
Basically what I changed was that I wanted to have different groupings of Polls; that's it. I made minimal changes to Question and Choice; so they are more applicable for a quiz application, but that's it.

Here are the models for Question and Choices:
from quiz_admin.models import Quiz

class Question(models.Model):
    def __str__(self):
        return self.question_text

    quiz = models.ForeignKey(Quiz)
    question_text = models.CharField(max_length=500)

class Choice(models.Model):
    def __str__(self):
        return self.choice_text

    question = models.ForeignKey(Question)
    choice_text = models.CharField(max_length=200)
    answer = models.BooleanField(default=False)

Open in new window


Here is the model for Quiz, this is in a separate app within the project:
class Quiz(models.Model):
    class Meta:
        verbose_name_plural = "quizzes"
    def __str__(self):
        return self.category_text

    groups = models.ManyToManyField(Group, related_name='group')
    category_text = models.CharField(max_length=200, unique=True )
    category_description = models.CharField(max_length=1000, default="")
    #url = models.URLField()
    course_code = models.CharField(max_length=40, default="CD"+datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S'))
    due_date = models.DateField(default=datetime.date.today)
    duration_hours = models.DecimalField(max_digits=5, decimal_places=2, default=0.0)
    required_score = models.DecimalField(max_digits=3, decimal_places=0, default=100)

Open in new window

0
 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 2000 total points
ID: 40574123
I'm not following you I think and my model broke ...
0
 

Author Comment

by:Errang Genevre
ID: 40574389
Heh, that's the problem...

Unless you have a foreign key relationship between Choice and Quiz; Django doesn't seem to want to link up the two tables together.

I'm trying to figure out a way to have the admin page auto generate the elements needed to perform CRUD operations, without having to have a foreign key between Choice and Quiz.
0
 

Author Comment

by:Errang Genevre
ID: 40575078
I think you included Question and Choice in the same admin page, right? Removing the relationship between Question and Quiz should fix it.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question