0

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
user3972986
  • 490
  • 1
  • 5
  • 14

2 Answers2

0

You can initialize your form with an assessment instance. See https://docs.djangoproject.com/en/1.7/topics/forms/modelforms/#modelform.

assessment = Assessment(location=location)
form = AssessmentForm(request.POST, request.FILES, instance=assessment)

Then, of course, you should remove the location field from your AssessmentForm.

Webthusiast
  • 986
  • 10
  • 16
0

your view has the information of the location for which the assessments needs to be displayed. you could add the location field as auto-selected by setting the value of location before rendering the form.

location = Location.objects.get(pk=location_id)
form.location = location

if you need the location field to be non-editable, follow this link

Community
  • 1
  • 1
srj
  • 9,591
  • 2
  • 23
  • 27
  • I am getting a 404 error when i post the form. Heres what i put in the template.
    – user3972986 Sep 18 '14 at 11:37
  • @user3972986, could you describe the error that you are getting? it could be because you are using `{{Location.location_id}}` where the variable Location is a class, not an instance. try using `location` variable. – srj Sep 19 '14 at 07:30
  • also, it is best practice not to use hard-coded urls, instead, you could use django's url tags in the form. that makes it easier to change the api urls without you having to muck around in all your template codes. – srj Sep 19 '14 at 07:33