I am having an issue when trying to edit
a form in a template with a regular form
and a formset
, I receive this error when I try to save the update rows. The page populates correctly. I'm not sure what the primary error is of this.
The traceback in python
is stating:
'JobProficienciesFormFormSet' object is not callable
At line 100: form = proficiencies_formset(request.POST)
Here is my code:
views.py
@login_required(login_url='/login/')
def edit(request, jobid):
# Get the job so we can edit. Also, for checking if the company logged in is valid
job = Jobs.objects.get(id=jobid)
# Permission check
if job.user_id == request.user.id:
# Get associated Job Proficiencies IDs attached to a Job
profid = JobProficiencies_Proficiencies.objects.filter(job_id=jobid).select_related('job_proficiencies_id')
# Get the Proficiency values attached to the job
qset = JobProficiencies.objects.filter(id__in=profid).select_related('job_proficiency', 'proficiency_required', 'comfort_level')
job_proficiencies = modelformset_factory(JobProficiencies, max_num=10, extra=0)
proficiencies_formset = job_proficiencies(queryset=qset)
args = {}
args.update(csrf(request))
args = {
'id': jobid,
'job': JobForm(instance=job),
'job_proficiencies_formset': proficiencies_formset,
}
if request.method == 'POST':
job = JobForm(request.POST, instance=job)
form = proficiencies_formset(request.POST)
if job.is_valid() and proficiencies_formset.is_valid():
m_tags = job.cleaned_data['m_tags']
_job = job.save(commit=False)
_job.id = jobid
_job.dateUpdated = datetime.now()
_job.save()
# Save proficiencies
for f in form:
job_proficiency = f.save(commit=False)
job_proficiency.save()
JobProficiencies_Proficiencies.objects.update(job_id=_job.id, job_proficiencies_id=job_proficiency.id)
# Without this next line the tags won't be saved.
for m_tag in m_tags:
job.tags.add(m_tag)
messages.success(request, "Job Edited!")
return HttpResponseRedirect('/job/all/')
else:
messages.error(request, "There are form errors!")
return render_to_response('job/edit.html/', args, context_instance=RequestContext(request))
return render_to_response('job/edit.html', args, context_instance=RequestContext(request))
HttpResponseRedirect('job/all/')
forms.py
class JobForm(forms.ModelForm):
m_tags = TagField()
class Meta:
model = Jobs
fields = ('title', 'about', 'wage', 'gitHubLink')
def save(self, commit=True):
job = super(JobForm, self).save(commit=False)
job.title = self.cleaned_data['title']
job.about = self.cleaned_data['about']
job.wage = self.cleaned_data['wage']
job.gitHubLink = self.cleaned_data['gitHubLink']
job.dateUpdated = datetime.now()
job.status = 'JO'
if commit:
job.save()
return job
class JobProficienciesForm(forms.ModelForm):
class Meta:
model = JobProficiencies
fields = ('job_proficiency', 'proficiency_required', 'comfort_level')
def save(self, commit=True):
jobproficiency = super(JobProficienciesForm, self).save(commit=False)
jobproficiency.job_proficiency = self.cleaned_data['job_proficiency']
jobproficiency.proficiency_required = self.cleaned_data['proficiency_required']
jobproficiency.comfort_level = self.cleaned_data['comfort_level']
if commit:
jobproficiency.save()
return jobproficiency
Again, the template populates correctly. The error is on saving the edit. That's where I am stuck. Any assistance is much appreciated. Thank you!
EDIT Here is how I'm displaying the formset in the template:
{{ job_proficiencies_formset.management_form }}
{% for p in job_proficiencies_formset %}
{{ p.id }}
<div class="row item">
<div class="col-md-5">
{{ p.job_proficiency|add_class:"form-control" }}
</div>
<div class="col-md-3">
{{ p.comfort_level|add_class:"form-control" }}
</div>
<div class="col-md-3">
{{ p.proficiency_required|add_class:"form-control" }}
</div>
<div class="col-md-1">
<p><a id="delete-row" class="delete-row" href="#"><i class="fa fa-trash-o fa-2x"></i></a></p>
</div>
</div>
{% endfor %}
At that point in the code, proficiencies_formset
is a formset object, not a class.
proficiencies_formset = job_proficiencies(queryset=qset)
...
form = proficiencies_formset(request.POST)
I think what you mean there is:
proficiencies_formset = job_proficiencies(request.POST)
And then later:
# Save proficiencies
for f in proficiencies_formset:
This kind of thing is one reason I like to maintain the convention of using capital letters to begin class names, even when the class is constructed by a factory.
I am having an issue when trying to edit
a form in a template with a regular form
and a formset
, I receive this error when I try to save the update rows. The page populates correctly. I'm not sure what the primary error is of this.
The traceback in python
is stating:
'JobProficienciesFormFormSet' object is not callable
At line 100: form = proficiencies_formset(request.POST)
Here is my code:
views.py
@login_required(login_url='/login/')
def edit(request, jobid):
# Get the job so we can edit. Also, for checking if the company logged in is valid
job = Jobs.objects.get(id=jobid)
# Permission check
if job.user_id == request.user.id:
# Get associated Job Proficiencies IDs attached to a Job
profid = JobProficiencies_Proficiencies.objects.filter(job_id=jobid).select_related('job_proficiencies_id')
# Get the Proficiency values attached to the job
qset = JobProficiencies.objects.filter(id__in=profid).select_related('job_proficiency', 'proficiency_required', 'comfort_level')
job_proficiencies = modelformset_factory(JobProficiencies, max_num=10, extra=0)
proficiencies_formset = job_proficiencies(queryset=qset)
args = {}
args.update(csrf(request))
args = {
'id': jobid,
'job': JobForm(instance=job),
'job_proficiencies_formset': proficiencies_formset,
}
if request.method == 'POST':
job = JobForm(request.POST, instance=job)
form = proficiencies_formset(request.POST)
if job.is_valid() and proficiencies_formset.is_valid():
m_tags = job.cleaned_data['m_tags']
_job = job.save(commit=False)
_job.id = jobid
_job.dateUpdated = datetime.now()
_job.save()
# Save proficiencies
for f in form:
job_proficiency = f.save(commit=False)
job_proficiency.save()
JobProficiencies_Proficiencies.objects.update(job_id=_job.id, job_proficiencies_id=job_proficiency.id)
# Without this next line the tags won't be saved.
for m_tag in m_tags:
job.tags.add(m_tag)
messages.success(request, "Job Edited!")
return HttpResponseRedirect('/job/all/')
else:
messages.error(request, "There are form errors!")
return render_to_response('job/edit.html/', args, context_instance=RequestContext(request))
return render_to_response('job/edit.html', args, context_instance=RequestContext(request))
HttpResponseRedirect('job/all/')
forms.py
class JobForm(forms.ModelForm):
m_tags = TagField()
class Meta:
model = Jobs
fields = ('title', 'about', 'wage', 'gitHubLink')
def save(self, commit=True):
job = super(JobForm, self).save(commit=False)
job.title = self.cleaned_data['title']
job.about = self.cleaned_data['about']
job.wage = self.cleaned_data['wage']
job.gitHubLink = self.cleaned_data['gitHubLink']
job.dateUpdated = datetime.now()
job.status = 'JO'
if commit:
job.save()
return job
class JobProficienciesForm(forms.ModelForm):
class Meta:
model = JobProficiencies
fields = ('job_proficiency', 'proficiency_required', 'comfort_level')
def save(self, commit=True):
jobproficiency = super(JobProficienciesForm, self).save(commit=False)
jobproficiency.job_proficiency = self.cleaned_data['job_proficiency']
jobproficiency.proficiency_required = self.cleaned_data['proficiency_required']
jobproficiency.comfort_level = self.cleaned_data['comfort_level']
if commit:
jobproficiency.save()
return jobproficiency
Again, the template populates correctly. The error is on saving the edit. That's where I am stuck. Any assistance is much appreciated. Thank you!
EDIT Here is how I'm displaying the formset in the template:
{{ job_proficiencies_formset.management_form }}
{% for p in job_proficiencies_formset %}
{{ p.id }}
<div class="row item">
<div class="col-md-5">
{{ p.job_proficiency|add_class:"form-control" }}
</div>
<div class="col-md-3">
{{ p.comfort_level|add_class:"form-control" }}
</div>
<div class="col-md-3">
{{ p.proficiency_required|add_class:"form-control" }}
</div>
<div class="col-md-1">
<p><a id="delete-row" class="delete-row" href="#"><i class="fa fa-trash-o fa-2x"></i></a></p>
</div>
</div>
{% endfor %}
At that point in the code, proficiencies_formset
is a formset object, not a class.
proficiencies_formset = job_proficiencies(queryset=qset)
...
form = proficiencies_formset(request.POST)
I think what you mean there is:
proficiencies_formset = job_proficiencies(request.POST)
And then later:
# Save proficiencies
for f in proficiencies_formset:
This kind of thing is one reason I like to maintain the convention of using capital letters to begin class names, even when the class is constructed by a factory.
0 commentaires:
Enregistrer un commentaire