This commit is contained in:
Ross
2021-05-05 22:16:54 +01:00
parent 69753d176c
commit 90995d7837
2 changed files with 44 additions and 20 deletions
+21 -19
View File
@@ -5,6 +5,7 @@ from django.forms import (
ModelChoiceField, ModelChoiceField,
ChoiceField, ChoiceField,
CharField, CharField,
HiddenInput,
) )
from django.forms import inlineformset_factory from django.forms import inlineformset_factory
@@ -32,34 +33,35 @@ class QuestionNoteForm(ModelForm):
class Meta: class Meta:
model = QuestionNote model = QuestionNote
fields = ["content_type", "object_id", "author", "note_type", "note"] fields = ["content_type", "object_id", "author", "note_type", "note"]
widgets = {'content_type': HiddenInput(), 'object_id': HiddenInput()}
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
question_type = kwargs.pop("question_type") question_type = kwargs.pop("question_type")
pk = kwargs.pop("pk") pk = kwargs.pop("pk")
if question_type == "rapid": #if question_type == "rapid":
question = Rapid # question = Rapid
content_type = ContentType.objects.get(model="rapid") # content_type = ContentType.objects.get(model="rapid")
elif question_type == "anatomy": #elif question_type == "anatomy":
question = AnatomyQuestion # question = AnatomyQuestion
content_type = ContentType.objects.get(model="anatomyquestion") # content_type = ContentType.objects.get(model="anatomyquestion")
elif question_type == "long": #elif question_type == "long":
question = Long # question = Long
content_type = ContentType.objects.get(model="long") # content_type = ContentType.objects.get(model="long")
else: #else:
raise PermissionError() # raise PermissionError()
get_object_or_404(question, pk=pk) #get_object_or_404(question, pk=pk)
# We get the 'initial' keyword argument or initialize it # We get the 'initial' keyword argument or initialize it
# as a dict if it didn't exist. # as a dict if it didn't exist.
#if kwargs.get("instance"): #if kwargs.get("instance"):
ModelForm.__init__(self, *args, **kwargs) ModelForm.__init__(self, *args, **kwargs)
super(QuestionNoteForm, self).__init__(*args, **kwargs) super(QuestionNoteForm, self).__init__(*args, **kwargs)
initial = kwargs.setdefault("initial", {}) #initial = kwargs.setdefault("initial", {})
# The widget for a ModelMultipleChoiceField expects ## The widget for a ModelMultipleChoiceField expects
# a list of primary key for the selected data. ## a list of primary key for the selected data.
#initial["content_type"] = question ##initial["content_type"] = question
#initial["content_id"] = pk ##initial["content_id"] = pk
self.fields["content_type"].initial = content_type #self.fields["content_type"].initial = content_type
self.fields["object_id"].initial = pk #self.fields["object_id"].initial = pk
+22
View File
@@ -162,8 +162,30 @@ class AddQuestionNote(LoginRequiredMixin, CreateView):
model = QuestionNote model = QuestionNote
form_class = QuestionNoteForm form_class = QuestionNoteForm
#fields = ("author", "note_type", "note")
def get_form_kwargs(self): def get_form_kwargs(self):
kwargs = super(AddQuestionNote, self).get_form_kwargs() kwargs = super(AddQuestionNote, self).get_form_kwargs()
# update the kwargs for the form init method with yours # update the kwargs for the form init method with yours
kwargs.update(self.kwargs) # self.kwargs contains all url conf params kwargs.update(self.kwargs) # self.kwargs contains all url conf params
return kwargs return kwargs
def get_initial(self):
pk = self.kwargs["pk"]
question_type = self.kwargs["question_type"]
if question_type == "rapid":
question = Rapid
content_type = ContentType.objects.get(model="rapid")
elif question_type == "anatomy":
question = AnatomyQuestion
content_type = ContentType.objects.get(model="anatomyquestion")
elif question_type == "long":
question = Long
content_type = ContentType.objects.get(model="long")
else:
raise PermissionError()
get_object_or_404(question, pk=pk)
return { "content_type" : content_type,
"object_id" : pk }