I'm using Django 4.0 and I'm trying to create a web app where the user selects a choice from a dropdown Django form. However, the choices will vary depending on the question and I want the form to be able to adapt to this.
This is what I have in forms.py:
class QuestionAnswerForm(forms.Form):
def __init__(self, q_id, *args, **kwargs):
self.q_id = q_id
super(QuestionAnswerForm, self).__init__(*args, **kwargs)
q_id = self.q_id # this line throws an error
question = Question.objects.get(pk=q_id)
choice = forms.ChoiceField(choices=get_choices(question))
However, I get the error: name 'self' not defined. I just want to know an easy way to pass the question id to the form so that the get_choices function can then return the choices that need to be displayed on the form.
In views.py, the start of my view for the question sets the form in this way:
def question_detail_view(request, q_id):
print(f"Question id is {q_id}")
form = QuestionAnswerForm(request.POST or None, q_id=q_id)
My question is: how do I access the q_id in the QuestionAnswerForm class?
I found out how to do it using Passing **kwargs to Django Form:
forms.py:
class QuestionAnswerForm(forms.Form):
def __init__(self, *args, **kwargs):
q_id = kwargs.pop('q_id')
super(QuestionAnswerForm, self).__init__(*args, **kwargs)
if q_id:
self.fields['choice'].choices = get_choices(Question.objects.get(pk=q_id))
choice = forms.ChoiceField()
views.py:
def question_detail_view(request, q_id):
form = QuestionAnswerForm(request.POST or None, q_id=q_id)
I believe one solution to this is that when creating your models:
Create question model
Create choice model and link it to a particular question
Related
I've been reading lots of questions like this on stackoverflow but none seem to work. All I want to do is make a filtered form dropdown. I'm not sure how do go about doing it. I get the error that main is not defined... but I'm sure that's because it's not initialized or something? I'm very confused lol.
My form code looks like this:
class AssignForm(ModelForm):
class Meta():
model = Address
fields = ['overseer','publisher', 'status']
def __init__(self, *args, **kwargs,):
super(AssignForm, self).__init__(*args, **kwargs)
self.fields['publisher'].queryset = Publisher.objects.filter(service_group=main)
Here is my View:
def assignment(request, pk_a):
assign = Address.objects.get(id=pk_a)
num = request.user.overseer.publisher_set.first()
main = num.service_group.id
print(main)
I would like to use the variable: main inside my form dropdown so I can limit the dropdown relative to the overseer. How can this be accomplished? Thanks!
form = AssignForm(main, request.POST, instance=assign)
context = {'form':form,}
return render(request, 'sms/assign.html', context )
Change your form to
class AssignForm(ModelForm):
class Meta():
model = Address
fields = ['overseer','publisher', 'status']
def __init__(self, main, *args, **kwargs,):
super(AssignForm, self).__init__(*args, **kwargs)
self.fields['publisher'].queryset = Publisher.objects.filter(service_group=main)
and change your Form instantiation in views to
form = AssignForm(main, request.POST, instance=assign)
I want to use UpdateView in my model Event. This model had this field:
employee = models.ForeignKey(User, on_delete=models.CASCADE, related_name='event_employee')
My view :
class UpdateEvent(UpdateView):
model = Event
template_name = 'dashboard/pro_update_event.html'
form_class = UpdateEventForm
other_variable = None
def get_form_kwargs(self):
kwargs = super(UpdateEvent, self).get_form_kwargs()
names_clients = User.objects.filter(professionnels=self.request.user)
kwargs.update({'names_clients': names_clients})
return kwargs
def get_success_url(self, *args, **kwargs):
return reverse_lazy('pro_details_event', kwargs={'pk': self.object.pk})
My Form :
class UpdateEventForm(forms.ModelForm):
"""
edit an event
"""
class Meta():
model = Event
fields = ('employee', 'date_start', 'date_end')
def __init__(self, names_clients, *args, **kwargs):
super(UpdateEventForm, self).__init__(*args, **kwargs)
self.fields['employee'] = forms.ChoiceField(choices=tuple([(client.pk,client.last_name.capitalize()+" "+client.first_name.capitalize()) for client in names_clients]))
It seems work, the widget "select" contain the correct values.
example : <option value="2">Dupond Jean</option>
But when I submit the form :
Cannot assign "'2'": "Event.employee" must be a "User" instance.
I don't understand because if remove "get_form_kwargs" in my view and "def init" in my form, the value passed is the same (the pk of the employee). It's works with this way.
But the problem is all employee are selectable and the username is display not the firstname and lastname.
It's because the employee ForeignKey on your Event model points to the User model, but you're populating the choice field for the employee field with Particulier model data.
Either change the foreign key to point to the Particulier model, or pass in Users to the form kwargs
Sorry, I forgot to fix this error in the code in this page.
But I have the error in my 1st comment, with populating the choice field with an User model data.
I have an issue with my Create view. I initialise it like this:
class OutputCreateView(LoginRequiredMixin, generic.CreateView):
template_name = 'rcapp/common_create_update.html'
form_class = OutputForm
model = Output
def get_context_data(self, **kwargs):
context = super(OutputCreateView, self).get_context_data(**kwargs)
# self.form_class.fields['activity_ref'].queryset = Activity.objects.filter(rc_ref=ResultsChain.objects.get(pk=self.kwargs['rc']).pk)
context['is_authenticated'] = self.request.user.is_authenticated
return context
def form_valid(self, form):
# code code code
return redirect("/portal/edit/" + str(self.kwargs['rc']) + "/#outputs-table")
I have a ForeignKey Field in my model and I wanted to filter options for current view.
My form is set like this:
class OutputForm(forms.ModelForm):
class Meta:
model = Output
fields = ['value', 'activity_ref']
widgets = {
'value': forms.Select(choices=(#Choises here
,), attrs={"onChange":'select_changed()', 'class':'selector'})
}
I need to change a queryset for the activity_ref field.
You can see a commented line in get_context_data, it's where I tried to do this. But it didn't work. How can I get what I need?
You need to pass the choices / queryset to your form.
in OutputCreateView
def get_form_kwargs(self, *args, **kwargs)
filter_key = self.kwargs['rc']).pk
return {'filter_key': key}
Like this, it will give an error in when your form gets created, because of the unexpected argument. To get around that and to make use of it, override the init method.
In your OutputForm
def __init__(self, *args, **kwargs)
kwargs.pop('filter_key')
super()._init__(*args, **kwargs)
self.fields['value'] = forms.Select(queryset=Activity.objects.filter(rc_ref=ResultsChain.objects.get(pk=self.kwargs['rc']).pk),
attrs={"onChange":'select_changed()', 'class':'selector'})
You don't need to set the widgets value, as it is being done in the init method.
I'm creating a view which loads differents forms according to an argument given using the getattr() function:
form = getattr(forms, service.form)
but in the form I need my username to filter my files, so I have this:
class MyForm(forms.Form):
filename = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple)
K = forms.CharField(label='K', max_length=1)
fullOut = forms.CharField(label='fullOut', max_length=1)
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.user = kwargs.pop('user', None)
self.fields['filename'].queryset = userFile.objects.filter(self.user)
The problem is that I don't know how to pass the 'request.user' in my getattr() funtion. I know that if it was static it should be something like:
form = MyForm(user=request.user)
But I have tried somethings like:
form = getattr(forms, service.form, user=request.user)
And it doesn't work.I'm trying this but any idea of how list user files in a form will be welcomed.
Thanks in advance.
This doesn't have anything to do with you using getattr, the problem is in your __init__ method. You need to pop user before calling super().
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
super(MyForm, self).__init__(*args, **kwargs)
You should instantiate the form as you usually do:
form = MyForm(user=request.user)
It doesn't matter whether MyForm is declared in the same module:
class MyForm(forms.Form):
my_field = forms.CharField()
form = MyForm(user=request.user)
or if you get the form class dynamically using getattr
MyForm = getattr(forms, service.form)
form = MyForm(user=request.user)
Database:
Document has many Sections, Sections has many Comments
On each document page, there is a comment form that lets you pick the section (using a ModelChoiceField). The problem is that the ModelChoiceField will contain ALL sections for all documents.
So to limit them I do this:
class CommentForm(ModelForm):
def __init__(self, *args, **kwargs):
super(CommentForm, self).__init__(*args, **kwargs)
if self.instance:
logger.debug(self.instance.document_id) # Prints "None"
self.fields['section'].queryset = Section.objects.filter(document=self.instance.document)
# ^^^ Throws DoesNotExist as self.instance.document is None
and my view is just:
form = CommentForm()
How do I pass CommentForm a document id?
Edit: Tried in my view:
d = Document.objects.get(id=id)
c = Comment(d)
form = CommentForm(c)
but document_id is still None in CommentForm
You can pass the document id when initialising the form:
class CommentForm(ModelForm):
def __init__(self, doc_id=None, *args, **kwargs):
if doc_id:
self.fields['section'].queryset = Section.objects.filter(document__id=doc_id)
and in the view
def my_view(request):
...
doc = Document.objects(...)
form = CommentForm(doc_id = doc.id)
EDIT
I edited the second line of the view, which I think deals with your comment? (make doc.id) a keyword arguement