2014-09-13 125 views
0

對不起,如果標題沒有意義,但我有點不確定它是什麼後。如何從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) }) 

我希望這是有道理的。

感謝

+0

我不明白你爲什麼需要'Assessment'和一個單獨的'Assessment1'模型... – 2014-09-13 15:26:59

+0

多個類別組成一個評估,並且可以有多個相同的類別到一個評估 – 2014-09-13 15:31:07

+0

您應該實施評估類別一個單獨的字段與['choices'](https://docs.djangoproject.com/en/dev/ref/models/fields/#choices)設置。這樣所有類型的評估都在同一個模型中,它們只是根據它們的類別字段值而不同。 – 2014-09-13 17:50:58

回答

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

我想我正在繞着這個完全錯誤的方向解釋它。基本上我想要的是一個簡單的一對多關係表,它自動指定評估所屬的位置 – 2014-09-13 15:44:23

+0

好的,什麼令我困惑的是評估1-2-3實例變量和Assessment1類,爲什麼你需要那些? – 2014-09-13 15:53:39

+0

評估1-2-3應該是一個大型評估的分類。一個地點可以有很多評估,每個評估都由不同的類別組成。那有意義嗎。謝謝你的方式。 – 2014-09-13 16:08:56