Trying to add the id of a parent field to the child foreign key field in my form. The user has a list of locations. From that location they can add an assessment of that location. Instead of the assessment form giving me a list of all locations, I would like it to auto select the right one
urls.py
url(r'^accounts/loggedin/locations/all/$', 'assessments.views.locations'),
url(r'^locations/get/(?P<location_id>\d+)/$', 'assessments.views.location'),
url(r'^accounts/loggedin/locations/create/$', 'assessments.views.create'),
url(r'^locations/get/(?P<location_id>\d+)/add_assessment/$', 'assessments.views.create_assessment'),
models.py
class Location(models.Model):
address = models.CharField()
def __unicode__(self):
return u'%s' % self.address
class Assessment(models.Model):
location = models.ForeignKey(Location)
title = models.CharField()
def __unicode__(self):
return u'%s' % self.location
views.py
def create_assessment(request, location_id):
location = Location.objects.get(pk=location_id)
if request.POST:
form = AssessmentForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return HttpResponseRedirect('/accounts/loggedin/assessments/all/')
else:
form = AssessmentForm()
args = {}
args.update(csrf(request))
args['form'] = form
return render_to_response('assessment/create_assessment.html', args,)
forms.py
class LocationForm(forms.ModelForm):
class Meta:
model = Location
class AssessmentForm(forms.ModelForm):
class Meta:
model = Assessment