allow variable number of screening questions

This commit is contained in:
aatish 2018-10-20 11:19:49 +05:45
parent 73aedd193b
commit 7b33b832fe
3 changed files with 33 additions and 2 deletions

View File

@ -25,3 +25,6 @@ class QuestionForm(forms.ModelForm):
QuestionFormSet = inlineformset_factory(
Job, Question, form=QuestionForm, can_delete=False, can_order=False)
QuestionUpdateFormSet = inlineformset_factory(
Job, Question, form=QuestionForm, can_delete=True, can_order=False)

View File

@ -55,7 +55,7 @@ class Job(models.Model):
class Question(models.Model):
''' A model to hold screening questions for a job post '''
name = models.TextField()
name = models.TextField('Question Title')
job = models.ForeignKey(
Job, related_name='questions', on_delete=models.CASCADE)

View File

@ -11,9 +11,37 @@
{{ form |crispy }}
<h4>Screening Questions</h4>
{{ question_form |crispy }}
<a id="add_more" href="">Add More</a><br/>
<input class="btn btn-primary submit" type="submit" value="Post Job"/>
</form>
{{form.media}}
</div>
</div>
<script>
// modified from https://stackoverflow.com/a/669982
function cloneMore(selector, type) {
var newElement = $(selector).clone(true);
var total = $('#id_' + type + '-TOTAL_FORMS').val();
newElement.find(':input').each(function () {
var name = $(this).attr('name').replace('-' + (total - 1) + '-', '-' + total + '-');
var id = 'id_' + name;
$(this).attr({ 'name': name, 'id': id }).val('').removeAttr('checked');
});
newElement.find('label').each(function () {
var newFor = $(this).attr('for').replace('-' + (total - 1) + '-', '-' + total + '-');
$(this).attr('for', newFor);
});
total++;
$('#id_' + type + '-TOTAL_FORMS').val(total);
$(selector).after(newElement);
}
$(document).ready(function() {
$('#add_more').click(function () {
cloneMore('.multiField:last', 'questions');
return false;
});
})
</script>
{% endblock %}