I have a model form which creates assignments, and each assignment needs to have some criteria, and so the next page in the creation process is a model formset which lets the user create multiple criteria at once. The problem I'm currently having is that I only want the user to add criteria to the assignment that they have just created, rather than using the dropdown menu and selecting the assignment manually.
I've read about modifying the queryset and it seems like the way to go but I',m not sure how I can get the value of just the newly created assignment.
The models:
# Assignment
class Assignment(models.Model):
assignmentName = models.CharField(max_length=100)
def __str__(self):
return self.assignmentName
def get_absolute_url(self):
return reverse('assignment-view')
#Criteria
class Criteria(models.Model):
assignmentID = models.ForeignKey(Assignment, on_delete=models.CASCADE)
criteriaDescription = models.TextField()
def __str__(self):
return self.criteriaDescription
Views: Assignment Create View
class assignmentCreateView(CreateView):
model = Assignment
template_name = 'markingApp/createAssignment.html'
fields = ["assignmentName"]
success_url = '/createCriteria'
criteriaCreateView
criteriaFormset= modelformset_factory(Criteria, fields=('assignmentID','criteriaDescription'))
form = criteriaFormset()
return render(request,'markingApp/createCriteria.html', {'form': form})
Once the user has created an assignment on the createAssignment.html page, they click next and the success_url redirects them to the page with the formset, allowing them to create criteria. Create Assignment Form
<form method='post'>
<div class="form-group">
{% csrf_token %}
{{form|crispy}}
</div>
<button type="submit" class="btn btn-primary">Next</button>
Create Criteria Form
<form method='post'>
<div class="form-group">
{% csrf_token %}
{{form|crispy}}
</div>
<button type="submit" class="btn btn-primary">Next</button>
Edit: I tried adding some code in the submission button to send the user to the URL of the assignment they create, but of course, the assignment doesn't exist yet so django throws an error since the reverse method is looking for nothing
<a href = "{% url 'criteria-create' Assignment.id %}" type="submit" class="btn btn-primary">Next</a>
70 </form>
> Reverse for 'criteria-create' with arguments '('',)' not found. 1
> pattern(s) tried: ['(?P<pk>[0-9]+)/createCriteria/\\Z']