I have created two url paths.
path('addhours/<int:jobNr>/', views.AddHoursView.as_view(), name='addhours'),
path('addhours/', views.AddHoursView.as_view(), name='addhours'),
And a CreateView for these paths.
class AddHoursView(generic.CreateView):
template_name = "worktime/addhours.html"
form_class = AddHoursForm
success_url = reverse_lazy('worktime:myjobs')
def get_form_kwargs(self):
# pass "jobNr" keyword argument from the current url to form
kwargs = super(AddHoursView, self).get_form_kwargs()
kwargs['jobNr'] = self.kwargs.pop(JOB_PARAM, None)
return kwargs
And a form, where the number of fields depends on if the parameter exists in the URL.
class AddHoursForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
#Add jobworker field to from Worktime model if any jobNr pass in url
#When in url will be parameter. Job foreignkey will be set by automat.
self.jobNr = kwargs.pop('jobNr', None)
super(AddHoursForm, self).__init__(*args, **kwargs)
if not self.jobNr:
self.fields['jobWorker'] = forms.ModelChoiceField(queryset=Job.objects.all())
class Meta:
model = WorkTime
fields = ['date', 'hours', 'description']
widgets = {
'date': forms.SelectDateWidget(empty_label=("Choose Year",
"Choose Month",
"Choose Day"))
}
Now I want to have in AddHoursView context["jobNr"] = "bar" where the URL is /url/bar
In AddHoursView's def context_data(self, **kwargs) I tried:
self.request.GET gives me empty QueryDict
self.kwargs['jobNr'] give me a key error
I need access to the url parameter if it exist, to pass this jobNr to template, so override form_valid is not what I need.
At the moment you are popping the value from self.kwargs, which removes it. That means it is no longer there when you try to access it in get_context_data. Since the argument is optional, you can use get() instead.
Change the method to:
def get_form_kwargs(self):
kwargs = super(AddHoursView, self).get_form_kwargs()
kwargs['jobNr'] = self.kwargs.get('JOB_PARAM')
return kwargs
Then in get_context_data you can do:
def get_context_data(self, **kwargs):
kwargs = super(AddHoursView, self).get_context_data(**kwargs)
kwargs['jobNr'] = self.kwargs.get('JOB_PARAM')
return kwargs
Related
I want to filter form fields querysets based on the user selected. Therefore, I want to pass user as argument to the form in order to filter fields querysets in the form's __init__ method. When I pass any arguments to the form I get the following error.
class UserDetailView(LoginRequiredMixin, FormMixin, DetailView):
model = TbUser
form_class = TbPeopleEntranceRightForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
form = TbPeopleEntranceRightForm(user=self.object)
context['form'] = form
return context
__init__() got an unexpected keyword argument 'user'
how do I pass the argument correctly, and how I get it in the __init__ form method?
Update:
class TbPeopleEntranceRightForm(forms.ModelForm):
def __init__(self, user, **kwargs):
super().__init__(**kwargs)
print(user)
# Use `self.user` here or in some other methods.
__init__() missing 1 required positional argument: 'user'
don't do it in get_context_data, it's made for something else.
Use get_form_kwargs instead:
class UserDetailView(LoginRequiredMixin, FormMixin, DetailView):
model = TbUser
form_class = TbPeopleEntranceRightForm
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["user"] = self.object
return kwargs
And in your forms.py:
def __init__(self, user=None, *args, **kwargs):
self.user = user
super().__init__(*args, **kwargs)
You need to add the parameter to the form's __init__ method:
class TbPeopleEntranceRightForm(forms.ModelForm):
...
def __init__(self, user=None, **kwargs):
super().__init__(**kwargs)
self.user = user
# Use `self.user` here or in some other methods.
Also, the correct way to then pass the user argument to the form is to override get_form_kwargs in the view, like #MojixCoder showed.
I have trouble setting up a form with a ModelMultipleChoiceField where the queryset depends on the user. My goal is to implement an export function.
My view looks like this:
class ExportView(FormView):
template_name = 'ExportTemplate.html'
def get(self, request, *args, **kwargs):
self.form_class = ExportForm(user = request.user)
return render(request, self.template_name, {'form': self.form_class})
def get_success_url(self):
return '/addrbook/'
def form_valid(self, form):
# This method is called when valid form data has been POSTed.
# It should return an HttpResponse.
return super().form_valid(form)
form:
class ExportForm(forms.Form):
def __init__(self, user, *args, **kwargs):
usersContacts = ContactManager().getAllUsersContacts()
self.contactList = forms.ModelMultipleChoiceField(queryset = usersContacts[str(user)])
print(usersContacts[str(user)])
super(ExportForm, self).__init__(*args, **kwargs)
I verified that the queryset is not empty, it contains a list of model objects.
My template looks like this:
<form method="post">{% csrf_token %}
{{ form }}
<input type="submit">
</form>
the only thing that gets rendered is the submit button.
Another thing that left me completely unsure of python basics is that this code:
class ExportForm(forms.Form):
contactList = forms.ModelMultipleChoiceField(queryset = [])
def __init__(self, user, *args, **kwargs):
usersContacts = ContactManager().getAllUsersContacts()
self.contactList.queryset = usersContacts[str(user)]
print(usersContacts[str(user)])
super(ExportForm, self).__init__(*args, **kwargs)
returned the runtime error:
'ExportForm' object has no attribute 'contactList'
How is it possible? the contactList member is part of the ExportForm class definition and 'self' should point to an object of that class.
Could someone explain to me why the form field is rendered empty and/or point me to a better way to pass the user to the form?
Edit: here are the changes i made to the answer to get it working, although i now stumbled upon a different problem(the field expects a queryset, and not a list of model objects):
View:
class ExportView(FormView):
template_name = 'ExportTemplate.html'
form_class = ExportForm
def get_form_kwargs(self):
kwargs = super(ExportView, self).get_form_kwargs()
kwargs['user'] = self.request.user
return kwargs
form:
class ExportForm(forms.Form):
contactList = forms.ModelMultipleChoiceField(queryset = Contact.objects.none())
def __init__(self, *args, **kwargs):
user = kwargs['user']
kwargs.pop('user', None)
super(ExportForm, self).__init__(*args, **kwargs)
usersContacts = ContactManager().getAllUsersContacts()
self.fields['contactList'].queryset = usersContacts[str(user)]
print(self.fields['contactList'].queryset)
First of all, you should pass the user to the form every time you instantiate it, not just in the get method. The way to do this with FormView is to override get_form_kwargs.
def get_form_kwargs(self):
kwargs = super(ExportForm, self).get_form_kwargs()
kwargs[user] = self.request.user
return kwargs
You can then remove your get() method.
Then, in your form class, you should use the none() method instead of an empty list to get an empty queryset. In the __init__ method you can pop the user from kwargs, and then call super() before you edit the fields. You edit the contactList field via self.fields instead of self.contactList. Note that the recommended style for field names in Django is contact_list instead of contactList.
class ExportForm(forms.Form):
contactList = forms.ModelMultipleChoiceField(queryset=YourModel.objects.none())
def __init__(self, *args, **kwargs):
user = kwargs.pop('user')
super(ExportForm, self).__init__(*args, **kwargs)
usersContacts = ContactManager().getAllUsersContacts()
self.fields['contactList'].queryset = usersContacts[str(user)]
You haven't shown the ContactManager() code, but using str(user) as the dictionary key looks fragile. It would probably be better to use user.pk or user.username instead.
My view have optional parameter in url. I pass this parameter (<int:jobNr>/None)to form in get_form_kwargs
class AddHoursView(LoginRequiredMixin, generic.CreateView):
form_class = AddHoursForm
def get_form_kwargs(self):
# pass "jobNr" keyword argument from current url to form
kwargs = super(AddHoursView, self).get_form_kwargs()
kwargs[JOB_PARAM] = self.kwargs.get(JOB_PARAM)
return kwargs
def form_valid(self, form):
self.object = form.save(commit = False)
# If url/jobNR take jobNr from URL, if not take from form
try:
jobNr = self.kwargs[JOB_PARAM]
except KeyError:
jobNr = form.cleaned_data[JOB_WORKER].jobNr
job = models.Job.objects.get(jobNr = jobNr)
jobWorker = models.JobWorker.objects.get_or_create(job = job,
user = self.request.user)
self.object.jobWorker = jobWorker[0]
self.object.save()
return HttpResponseRedirect(reverse('worktime:myjobs'))
In form __init__ If url /foo/jobNr/ create self.jobNr with url parameter. If url /foo/ create new field.
class AddHoursForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
#Add jobworker field to from Worktime model if any jobNr pass in url
#When in url will be parameter. Job foreignkey will be set by automat.
self.jobNr = kwargs.pop(JOB_PARAM, None)
super(AddHoursForm, self).__init__(*args, **kwargs)
if not self.jobNr:
self.fields[JOB_WORKER] = forms.ModelChoiceField(queryset=Job.objects.all())
def clean(self):
cleaned_data = super().clean()
date = cleaned_data.get('date')
if self.jobNr:
jobDate = Job.objects.get(jobNr=self.jobNr).start
elif not cleaned_data.get(JOB_WORKER).start:
raise forms.ValidationError("Dat work don't start yet")
else:
jobDate = cleaned_data.get(JOB_WORKER).start
if date<jobDate:
raise forms.ValidationError("Wrong date")
return cleaned_data
And now i want to code tests for a form. This is what i tried
def test_no_jobNr_in_url_and_no_date(self):
job = Job.objects.create(jobNr = 1, street = 'a', city = 'a',
zip = 'a')
form_data = {'description': 'description', 'hours': 20}
form = AddHoursForm(form_data)
# Override field jobWorker after __init__ form
form.fields['jobWorker'] = job
self.assertFalse(form.is_valid())
And this is what i get AttributeError: 'Job' object has no attribute 'disabled' After 5h i need help. I have no idea anymore.
you can't assign job to a form field (that's what form.fields['jobWorker'] is). If you want to assign an initial value, you should do:
form.fields['jobWorker'].initial = job
But I think you want to pass job as the chosen model, you should just do that in data:
form_data = {'description': 'description', 'hours': 20, 'jobWorker': job.pk}
form = AddHoursForm(form_data)
self.assertFalse(form.is_valid)
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 attempting to update a choice field depending on the user permissions. I have a boolean field which if False (default) the standard user gets to see. Otherwise if a user has the permission I want to display everything.
views.py
class ExportFormView(FormView):
template_name = 'export.html'
form_class = ExportForm
success_url = '/'
def get_form_kwargs(self):
kwargs = super(ExportFormView, self).get_form_kwargs()
kwargs.update({
'request' : self.request
})
return kwargs
forms.py
class ExportForm(forms.Form):
def __init__(self, request, *args, **kwargs):
self.request = request
super(ExportForm, self).__init__(*args, **kwargs)
choice_list = []
if request.user.has_perms('tracker.print_all'):
e = Muid.objects.values('batch_number').distinct()
else:
e = Muid.objects.values('batch_number').distinct().filter(printed=False)
for item in e:
choice = item['batch_number']
choice_list.append((choice, choice))
batch_number = forms.ChoiceField(choices = choice_list)
Error I get:
NameError at /
name 'request' is not defined
Any help would be greatly appreciated, I've been stuck on this for a while now (And tried many googled SO suggestions/Answers.)
Found out how to do it, still interested in other ways.
Using pdb, I found that the view was setup correctly. But I had to alter the form. I couldn't access the variable from outside of a function such as __init__, others functions should be able to access the variable too, but I needed to create the form on init, so I couldn't wait for function calls.
Code:
Views.py
class ExportFormView(FormView):
template_name = 'export_muids.html'
form_class = ExportForm
success_url = '/'
def get_form_kwargs(self):
kwargs = super(ExportFormView, self).get_form_kwargs()
kwargs.update({
'request' : self.request
})
return kwargs
forms.py
class ExportForm(forms.Form):
def __init__(self, request, *args, **kwargs):
self.request = request
choice_list = []
if request.user.has_perms('tracker.print_all'):
e = Muid.objects.values('batch_number').distinct()
else:
e = Muid.objects.values('batch_number').distinct().filter(exported=False)
for item in e:
choice = item['batch_number']
choice_list.append((choice, choice))
super(ExportForm, self).__init__(*args, **kwargs)
self.fields['batch_number'] = forms.ChoiceField(choices = choice_list)