如何从url获取id以在关系数据库中使用?

时间:2014-09-13 15:19:25

标签: python django

对不起,如果标题没有意义,但我有点不确定我追求的是什么。

我有一个列出所有位置的位置页面,您可以点击该页面以显示该位置的更多信息,并将该位置的ID放入网址。从位置页面我希望能够将多个评估添加到该位置。我在评估模型中添加了一对多关系字段,但会显示一个下拉菜单。如果从位置页面点击评估链接,我希望它自动将评估添加到该位置。

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) })

我希望这是有道理的。

由于

1 个答案:

答案 0 :(得分:1)

您应该重新构建模型:

class Location(model.Models):
    ...

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

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

让您的路线更像这样:

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'),

然后,更容易识别视图中位置,评估和类别的ID:

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})