If I place a queryset within the ModelForm on a field then the formset fails to save, without the query the formset works correctly.
I’d be grateful of advice on where I’m going wrong with the query as the html form and the POST data looks valid.
view:
def updateRaceParticipants(request, raceid):
template = loader.get_template('ksail_events/race_participant_update_form.html')
raceobj = Race.objects.get(id = raceid)
stageid = getattr(raceobj, 'stage_id')
stageobj = Stage.objects.get(id = stageid)
eventid = getattr(stageobj, 'event_id')
rp1formset = modelformset_factory(RaceParticipant, form=UpdateRaceParticipantForm, extra=0)
rp2formset = rp1formset(queryset=RaceParticipant.objects.filter(race_id = raceid), form_kwargs={"stageid": stageid})
myevent = Event.objects.get(id=eventid)
context = {
'form': rp2formset,
'myevent': myevent,
'myperms':myperms,
}
# If this is a POST request then process the Form data
if request.method == 'POST':
formset = rp1formset(request.POST)
#Check if the form is valid:
if formset.is_valid():
# process the data in form.cleaned_data as required (here we just write it to the model due_back field)
if (myperms.event_perm == 'WRITE' or request.user.is_staff or request.user.is_superuser):
formset.save()
else:
raise Exception(formset.errors)
# redirect to a new URL:
return HttpResponseRedirect(reverse('RaceDetailView', args=[eventid]))
# If this is a GET (or any other method) create the default form.
else:
formset = rp1formset(queryset=RaceParticipant.objects.filter(race_id = raceid), form_kwargs={"stageid": stageid})
return HttpResponse(template.render(context, request))
form:
class UpdateRaceParticipantForm(forms.ModelForm):
class Meta:
model = RaceParticipant
fields = ['boat', 'team', 'race', 'sailor','sched_side', 'finish_position', 'score_code', 'points',]
def __init__(self, *args, **kwargs):
stageid = kwargs.pop('stageid', None) # Extract ID from kwargs
super().__init__(*args, **kwargs)
#restrict the queries for the fields to just the data from this event
sf = StageFlightMapping.objects.filter(stage_id = stageid).values_list('stageflights_id')
self.fields['boat'].queryset = Boat.objects.filter(flight__in=sf).order_by('name')
st = StageTeamMapping.objects.filter(stage_id = stageid).values('enteredteam_id')
self.fields['team'].queryset = Team.objects.filter(id__in=st).order_by('name')
self.fields['race'].queryset = Race.objects.filter(stage_id = stageid).order_by('race_number')
Exception error (repeated 6 times once for each form in set):
[{'boat': ['Select a valid choice. That choice is not one of the available choices.'], 'team': ['Select a valid choice. That choice is not one of the available choices.'], 'race': ['Select a valid choice. That choice is not one of the available choices.']},
POST data (only first 2 forms shown):
csrfmiddlewaretoken
'mBbE6rl2Syz8ZeDHcilBTxQkRrOYver6JWgPQHlm0Bkwy55xCDP9VagA6wClXbvS'
form-TOTAL_FORMS
'6'
form-INITIAL_FORMS
'6'
form-MIN_NUM_FORMS
'0'
form-MAX_NUM_FORMS
'1000'
form-0-boat
'19'
form-0-team
'1'
form-0-race
'947'
form-0-sailor
''
form-0-sched_side
'LEFT'
form-0-finish_position
'1'
form-0-score_code
'NON'
form-0-points
'1'
form-0-id
'4368'
form-1-boat
'19'
form-1-team
'1'
form-1-race
'947'
form-1-sailor
''
form-1-sched_side
'LEFT'
form-1-finish_position
'2'
form-1-score_code
'NON'
form-1-points
'2'
form-1-id
'4369'
Many thanks
Paul