Last active
October 6, 2021 07:25
-
-
Save k4ml/2328cae83e67f76f954a02905d240076 to your computer and use it in GitHub Desktop.
Le-Wagon Workshop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib import admin | |
from mysite.models import Survey, Question, Choice | |
class QuestionInline(admin.TabularInline): | |
model = Question | |
show_change_link = True | |
class ChoiceInline(admin.TabularInline): | |
model = Choice | |
class SurveyAdmin(admin.ModelAdmin): | |
inlines = [ | |
QuestionInline | |
] | |
class QuestionAdmin(admin.ModelAdmin): | |
inlines = [ | |
ChoiceInline | |
] | |
admin.site.register(Survey, SurveyAdmin) | |
admin.site.register(Question, QuestionAdmin) | |
admin.site.register(Choice) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
class SurveyForm(forms.Form): | |
question_1 = forms.ChoiceField(widget=forms.RadioSelect, choices=()) | |
def __init__(self, survey, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
for question in survey.question_set.all(): | |
answers = [(answer.text, answer.text) for answer in question.choice_set.all()] | |
self.fields[f"question_{question.id}"] = forms.ChoiceField(widget=forms.RadioSelect, choices=answers) | |
self.fields[f"question_{question.id}"].label = question.text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
class Question(models.Model): | |
question_text = models.CharField(max_length=200) | |
pub_date = models.DateTimeField('date published') | |
def __str__(self): | |
return self.question_text | |
class Choice(models.Model): | |
question = models.ForeignKey(Question, on_delete=models.CASCADE) | |
choice_text = models.CharField(max_length=200) | |
votes = models.IntegerField(default=0) | |
def __str__(self): | |
return self.choice_text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mysite.models import Survey, Question, Choice, Submission | |
survey1 = Survey.objects.create(title="Survey 1") | |
q1 = Question.objects.create(survey=survey1, text="What is capital of Japan?", pub_date=timezone.now()) | |
q1.choice_set.add(Choice.objects.create(text="Tokyo")) | |
sub = Submission(survey=s1, participant_email="[email protected]", participant_phonenumber="+60187884325") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mysite.forms import SurveyForm | |
from mysite.models import Survey | |
survey = Survey.objects.all()[0] | |
data = { | |
"email": "[email protected]", | |
"phonenumber": "+81919191919", | |
"question_1": "1", | |
"question_2": "4", | |
} | |
form = SurveyForm(survey, data) | |
if form.is_valid(): | |
print(form.cleaned_data) | |
else: | |
print(form.errors) | |
print(form.fields["question_2"].choices) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment