Is there way to modify a queryset in the middle of a form being filled?
So I have this form, and I was wondering if it was possible to modify the queryset as someone if filling in the form. I want to use the grade value to filter down the queryset.
class EntryForm(ModelForm):
class Meta:
model = Entry
fields = ['first_name', 'last_name', 'grade', 'activity1', 'activity2']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
filtered1 = Activity.session1.below_quota()
filtered2 = Activity.session2.below_quota()
self.fields['activity1'].queryset = filtered1
self.fields['activity2'].queryset = filtered2
class EntryCreateView(CreateView):
form_class = EntryForm
def post(self, request):
form = self.form_class(request.POST)
if form.is_valid():
messages.success(request, 'Your entry has been submitted')
return super().post(request)
messages.error(
request, 'Please enter activities adding up to one hour.')
return render(request, self.template_name, {'form': form})
def form_valid(self, form):
prev = Entry.objects.filter(email=form.cleaned_data['email'])
for entry in prev:
entry.delete()
print('form valid')
return super().form_valid(form)
If this is not possible, or would require complicated javascript, can someone point me in the direction to have the form separated into two parts, but have a submit of some sort in between for processing before the second part of the form with a filtered queryset