Chris Meek
Chris Meek

Reputation: 1523

How to get id from url to use in relationship database?

Sorry if the title does not make sense but I am a little unsure what it is i am after.

I have a locations page that list all the locations, which you can click on to bring up more info on that location and puts the id of the location into the url. From the location page I want to be able to add multiple assessments to that location. I have added a one to many relationship field in the assessment model but that brings up a drop down menu. I want it to automatically add the assessment to the location if the assessment link was clicked from the location page.

url.py

    url(r'^accounts/loggedin/locations/all/$', 'assessments.views.locations'),
    url(r'^locations/get/(?P<location_id>\d+)/$', 'assessments.views.location'),
    url(r'^locations/get/(?P<location_id>\d+)/assessments/all/$', 'assessments.views.assessments'),
    url(r'^locations/get/(?P<location_id>\d+)/assessment1/(?P<assessment1_id>\d+)/$', 'assessments.views.assessment1'),

modules.py

class Location(model.Models):
    # some fields on the location

class Assessment(model.Models):
    location = models.ForeignKey(Location)
    assessment1 = models.CharField()
    assessment2 = models.CharField()
    assessment3 = models.CharField()

class Assessment1(model.Models):
    assessment = models.ForeignKey(Assessment)
    # some fields in assessment

ect

views.py

def locations(request):
    return render_to_response('dashboard/locations.html', {'locations': Location.objects.all() })

def location(request, location_id=1):
    return render_to_response('dashboard/location.html', {'location': Location.objects.get(id=location_id) })

def assessments(request):
    return render_to_response('dashboard/assessments.html', {'assessments': Assessments.objects.all() })

def assessments1(request, assessment1_id=1):
    return render_to_response('dashboard/assessment1.html', {'assessment1': Assessment1.objects.get(id=assessment1_id) })

I hope this makes sense.

Thanks

Upvotes: 0

Views: 130

Answers (1)

Mattias Farnemyhr
Mattias Farnemyhr

Reputation: 4248

You should restructure your models like this:

class Location(model.Models):
    ...

class Assessment(model.Models):
    location = models.ForeignKey(Location)
    ...

class Category(model.Models):
    assessment = models.ForeignKey(Assessment)
    ...

And have your routes be more like this:

url(r'^locations/$', 'views.locations'),
url(r'^locations/(?P<location_id>\d+)$', 'views.location'),
url(r'^locations/(?P<location_id>\d+)/assessments$', 'views.assessments'),
url(r'^locations/(?P<location_id>\d+)/assessments/(?P<assessment_id>\d+)$', 'views.assessment'),
url(r'^locations/(?P<location_id>\d+)/assessments/(?P<assessment_id>\d+)/categories$', 'views.categories'),

Then it will be much easier identifying the ids of the locations, assessments and categories in the views:

def locations(request):
    locations = Location.objects.all()
    return render_to_response('dashboard/locations.html', {'locations': locations})

def location(request, location_id):
    location = Location.objects.get(pk=location_id)
    return render_to_response('dashboard/location.html', {'location': location})

def assessments(request, location_id):
    location = Location.objects.get(pk=location_id)
    assessments = Assessment.objects.filter(location=location)
    return render_to_response('dashboard/assessments.html', {'assessments': assessments})

def assessment(request, location_id, assessment_id):
    location = Location.objects.get(pk=location_id)
    assessment = Assessment.objects.get(pk=assessment_id, location=location)
    return render_to_response('dashboard/assessment.html', {'assessment': assessment})

def categories(request, location_id, assessment_id):
    location = Location.objects.get(pk=location_id)
    assessment = Assessment.objects.get(pk=assessment_id, location=location)
    categories = Category.objects.filter(assessment=assessment)
    return render_to_response('dashboard/categories.html', {'categories': categories})

Upvotes: 1

Related Questions