對不起,如果標題沒有意義,但我有點不確定它是什麼後。如何從url中獲取id以用於關係數據庫?
我有一個位置頁面列出了所有位置,您可以點擊該位置以顯示該位置的更多信息,並將該位置的id放入url中。從位置頁面我希望能夠添加多個評估到該位置。我在評估模型中添加了一對多關係字段,但是會顯示一個下拉菜單。如果評估鏈接是從地點頁面點擊的,我希望它會自動將評估添加到該地點。
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) })
我希望這是有道理的。
感謝
我不明白你爲什麼需要'Assessment'和一個單獨的'Assessment1'模型... – 2014-09-13 15:26:59
多個類別組成一個評估,並且可以有多個相同的類別到一個評估 – 2014-09-13 15:31:07
您應該實施評估類別一個單獨的字段與['choices'](https://docs.djangoproject.com/en/dev/ref/models/fields/#choices)設置。這樣所有類型的評估都在同一個模型中,它們只是根據它們的類別字段值而不同。 – 2014-09-13 17:50:58