using views to provide queryset to a form

Date: April 19th 2016
Last updated: April 19th 2016

I want to provide a form that limits the number of options based on where a user lives. That is, after entering a city in their profile page I want the local beach select form to only show beaches that are in their city.

def selectbeach(request, surfer_id):
    surfer = get_object_or_404(Surfer, pk=surfer_id)
    form = BeachSelectForm(request.POST or None, instance=surfer)
    # =========== Solution ============================================
    form.fields['homebeach'].queryset = Beach.objects.filter(
    # after a form is created, modify the chosen field by filtering 
    # the model object. 
    # Refer back to to see where the links are made.
    # Beach has a foreign key with a table called Nearestcity which is referenced
    # by the field name 'city'. 
    # Each surfer also has a foreign key to the table NearestCity which is also
    # referenced by a field called city.
    # =================================================================
    if form.is_valid():
        return redirect('/{}/'.format(surfer_id))
    return render(request, 'surferprofile/selectbeach.html',
            {'surfer': surfer, 'form': form})

results matching ""

    No results matching ""