Is it possible to fetch foreign key items automatically from JobDetailView
to ApplyView
. Like When someone seeing JobDetailsView
then click on Apply Button and redirected to ApplyView
and the Designation Input was automatically filled based on what Job he was looking on previous page.
Models
# Jobs Model
class Job(models.Model):
jobid = models.SlugField(unique=True, editable=False, verbose_name="JobID")
title = models.CharField(max_length=250, verbose_name="Job Title")
country = models.CharField(max_length=250, verbose_name="Country")
description = fields.RichTextField(max_length=10000, verbose_name="Job Description")
keywords = models.CharField(max_length=500, blank=True, verbose_name="Keywords", default="Jobs, ")
posted = models.DateTimeField(auto_now_add=True, editable=False)
def save(self):
if not self.jobid:
self.jobid = 'JB' + customId()
super(Job, self).save()
def __str__(self):
return f'{self.title} ({self.jobid})'
class Meta:
verbose_name = "Vacancy"
verbose_name_plural = "Vacancies"
ordering = ('-posted',)
# Candidates Model
class Candidate(models.Model):
candidateid = models.SlugField(unique=True, editable=False, verbose_name="Candidate ID")
full_name = models.CharField(max_length=250, verbose_name="Full Name")
designation = models.ForeignKey(Job, on_delete=models.CASCADE, verbose_name="Designation")
total_experience = models.PositiveIntegerField(verbose_name="Total Experience")
email = models.EmailField(blank=True, verbose_name="Email")
mobile = models.CharField(max_length=250, verbose_name="Mobile No.")
location = models.CharField(max_length=250, verbose_name="Current Location")
summary = fields.RichTextField(max_length=1500, blank=True, verbose_name="Summary")
resume = files.RestrictedFileField(upload_to=files.renamecv, validators=[files.filesize], verbose_name="Upload Resume")
applied = models.DateTimeField(auto_now_add=True, editable=False)
keywords = models.CharField(max_length=500, blank=True, verbose_name="Keywords")
def save(self):
if not self.candidateid:
self.candidateid = 'CA' + customId()
super(Candidate, self).save()
def __str__(self):
return f'{self.full_name}, {self.designation}'
class Meta:
verbose_name = "Candidate"
verbose_name_plural = "Candidates"
ordering = ('-applied',)
Forms
from captcha import fields
class JobForm(ModelForm):
class Meta:
model = models.Job
fields = '__all__'
exclude = ['keywords',]
class CandidateForm(ModelForm):
captcha = fields.CaptchaField()
class Meta:
model = models.Candidate
fields = '__all__'
exclude = ['keywords',]
Views
# Job Views
class JobCreateView(mixins.LoginRequiredMixin, generic.edit.CreateView):
form_class = forms.JobForm
template_name = 'dash/jobForm.html'
slug_field = 'jobid'
slug_url_kwarg = 'jobid'
def form_valid(self, form):
item = form.save()
self.jobid = item.jobid
return super(JobCreateView, self).form_valid(form)
def get_success_url(self):
return reverse('jobdetail', kwargs={'jobid': self.jobid})
class JobDetailView(mixins.LoginRequiredMixin, generic.DetailView):
model = models.Job
context_object_name = 'job'
template_name = 'dash/jobDetail.html'
slug_field = 'jobid'
slug_url_kwarg = 'jobid'
# Apply Views
class ApplyView(generic.edit.CreateView):
form_class = forms.CandidateForm
template_name = 'pages/apply.html'
slug_field = 'candidateid'
slug_url_kwarg = 'candidateid'
def form_valid(self, form):
item = form.save()
self.candidateid = item.candidateid
return super(ApplyView, self).form_valid(form)
def get_success_url(self):
return reverse('applysuccess', kwargs={'candidateid': self.candidateid})
My URLs are like this FYI
path('viewjobs/<slug:jobid>', views.JobDetailView.as_view(), name="jobdetail"),