Django Admin - How can i add more than one related object? - python

For example i have this code
app/models.py
from django.db import models
class Poll(models.Model):
title = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Question(models.Model):
poll = models.ForeignKey(Poll)
question_text = models.CharField(max_length=200)
class Choice(models.Model):
question = models.ForeignKey(Question)
choice_text = models.CharField(max_length=200)
At first, I can add in Django Admin Poll, next i can add some question and answers to question, but i can do it only after creating poll and i can add only one question and answers to it at first time. How i can do it in one page while creating or updating Poll and add more than one question+answers?

Related

Django: How does model class take constructor arguments when no __init__() is defined?

In my app polls, we have the file models.py:
from __future__ import unicode_literals
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
And after starting a shell with python manage.py shell, we can write this:
from polls.models import Question
q = Question(question_text = "What's new?", pub_date=timezone.now())
The Question class is clearly taking constructor arguments when an object is created. But in our class definition of Question in models.py, there is no specification of any constructor arguments. So how does this work?
As Question class inherits models.Model class, it gets the constructor from there itself.

Counting Boolean Values in field of Child Model in Django QuerySet API

I have the following model (below).
I would like a query that returns every record of parent Question(models.Model), where ALL the values for the is_relevant field within the child Choice(models.Model) are "True".
Having a difficult time creating the QuerySet. Any Assistance would be very helpful.
model.py
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
is_relevant = models.BooleanField()
You can use the reverse relationship of the Question model like so:
Question.objects.filter(choice__is_relevant=True)
The choice in choice__is_relevant is the lowercase name of the model Choice.
Reference: Relationship lookups

Django form for model referenced by foreign keys

I am creating a quiz application in django. As of right now, I have two models, Question and Answer.
class Question(models.Model):
question_text = models.CharField(max_length=255)
video_ref = models.ForeignKey('Video')
class Answer(models.Model):
question_ref = models.ForeignKey('Question')
answer_text = models.CharField(max_length=255)
correct = models.BooleanField()
This setup allows me to have variable numbers of potential answers to every question with a possibility of multiple correct answers.
I want to create a form model for a Question such that every Answer that points to the Question is part of the form. How can this be done?
You can do as follow:
forms.py
class QuestionForm(forms.ModelForm):
answers = forms.ModelMultipleChoiceField(queryset=Question.answer_set.all(), widget=forms.CheckboxSelectMultiple)
class Meta:
model = Question
Using forms.ModelMultipleChoiceField and the widget forms.CheckboxSelectMultiple django will render the form with checkbox for you.

Setting up default ForeignKey in django models

I need to set up default Foreign Key for a model at the model declaration stage (in models.py). I use the following code:
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return str(self.pub_date)
def create_question():
q=Question(question_text='default text', pub_date=datetime.now())
q.save()
return q.id
class Choice(models.Model):
question = models.ForeignKey(Question, default=lambda: create_question())
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
This works fine, the Choice instance and associated Question instance are created... But saving of Choice instance leads to creation of two additional (useless) Question instances. It seems like that default-lambda is called several times during Choice instance saving. I don't need these additional Question instances. How to avoid their creation?
NOTE: In python-shell everything works well:
c=Choice(votes=5, choice_text='dim')
c.save()
creates one instance of Choice and one associated instance of Question. Only using admin save button leads to extra Question instances creation!

Django Help: AttributeError: 'module' object has no attribute 'Charfield'

I have seen several similar posts of other attributes found but not this. New to Python and Django- I've done the first part of the several tutorials including Django's "Polls" tutorial and when it gets to the point where I syncdb for my app I invariably get 'AttributeError: 'module' object has no attribute CharField.
In models I have copied exactly as the tutorial says:
from django.db import models
class Poll(models.Model):
question = models.Charfield(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
# Create your models here.
'polls' is also added to installed apps and I'm using sqlite3, windows 7, python 2.7.
Any help very appreciated! (I'm trying very hard to learn!)
That is CharField, with uppercase 'f', and not Charfield as in your code.
I think in forms.py you are using
from django.forms import forms
Please use this
from django import forms
change charfield to:
CharField(max_length = 10)
both C and F should be capitalized
This :
question = models.CharField(max_length=200)
Instead of :
question = models.Charfield(max_length=200)
I have the same error but following code works for me:
from django.db import models
#Create your models here.
class Question(models.Model):
question_text = models.CharField(max_length=100)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
choice_text = models.CharField(max_length = 200)
votes = models.IntegerField(default =0)
question = models.ForeignKey(Question, on_delete=models.CASCADE)
Simply change charfield() to charField() ..
In model Poll, spelling of CharField is not properly formatted. Ie you have written a small letter f inplace of a capital letter F. So, replace Charfield by CharField. You can see the code below:
from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published') class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
Use this
from django.db import models

Categories