0

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 assignment

Create Criteria Form

<form method='post'>
  <div class="form-group">
    {% csrf_token %}
    {{form|crispy}}
  </div>
  
  <button type="submit" class="btn btn-primary">Next</button>

Create Criteria

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']

1 Answers1

1

This view you pasted is a bit too small to see how it works. You need to redirect user to new page after Assignment is created, with that Assignment ID. Just call assignment = Assignment.save() on the view where Assignment is created and later on use assignment.id on the redirection.

jacoor
  • 760
  • 5
  • 9
  • ah sorrry if it was a bit vague. I'll add some more detail about the redirection now, the main issue I'm having is figuring out how to pass the value from the previous page of createAssignment.html to the createCriteria.html – BaileyLylyk Mar 21 '23 at 18:18
  • https://stackoverflow.com/questions/13309479/django-createview-how-to-get-the-object-that-is-created this might be an answer for you. – jacoor Mar 22 '23 at 20:14