2013-05-08 60 views
0

我試圖做一個鏈接的選擇菜單,我有這個模型:Django的ChainedForeignKey智能菜單

from django.db import models 

class Health_plan(models.Model): 
    name = models.CharField(max_length=15) 

class Doctors_list(models.Model): 
    name = models.CharField(max_length=30) 
    specialty = models.CharField(max_length=15) 
    health_plans = models.ManyToManyField(Health_plan, related_name="doctors") 
    location = models.CharField(max_length=15) 

    def __unicode__(self): 
     return self.name 

這是我forms.py

class SpecForm(ModelForm): 
    a = Doctors_list.objects.values_list('specialty', flat=True) 
    unique = [('---------------','---------------')] + [(i,i) for i in set(a)] 
    specialty = forms.ChoiceField(choices=unique) 
    class Meta: 
     model = Doctors_list 

class HealthForm(ModelForm): 
    hplan = ChainedForeignKey(
     Health_plan, 
     chained_field="specialty", 
     chained_model_field="specialty", 
     show_all=False, 
     auto_choose=True 
    ) 

urls.py

from django.conf.urls import patterns, include, url 
from testApp.views import spec_form 
from testApp.views import health_form 
from django.contrib import admin 
admin.autodiscover() 

urlpatterns = patterns('', 
    # Examples: 
    url(r'^$', 'Medbook.views.home', name='home'), 
    # url(r'^Medbook/', include('Medbook.foo.urls')), 
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')), 
    url(r'^admin/', include(admin.site.urls)), 
    url(r'^hello/$', spec_form, health_form), 
) 

and my views.py

from django.shortcuts import render_to_response 
from testApp.forms import SpecForm 
from testApp.forms import HealthForm 

def spec_form (request): 
    if request.method == 'POST': 
     form = SpecForm(request.POST) 
     if form.is_valid(): 
      form.save() 
    else: 
     form = SpecForm() 
    return render_to_response('hello.html', {'form':form}) 

def health_form (request): 
    if request.method == 'POST': 
     form = HealthForm(request.POST) 
     if form.is_valid(): 
      form.save() 
    else: 
     form = SpecForm() 
    return render_to_response('hello.html', {'form':form}) 

現在我得到的錯誤是'function' object is not iterable當我訪問的網頁。 我是Django的新手,我覺得這很棘手。用戶必須選擇一個專業,然後出現涵蓋該專業的health_plans。

health_plans與醫生有許多關係。當一個人選擇一個專業時,腳本應該檢查哪個醫生屬於該專業,並檢索這些醫生持有的所有健康計劃。

我希望我明確表示,因爲我的代碼不是。

任何幫助,懇請讚賞

編輯:堆棧跟蹤

Internal Server Error: /hello/ Traceback (most recent call last): File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 103, in get_response resolver_match = resolver.resolve(request.path_info) File "C:\Python27\lib\site-packages\django\core\urlresolvers.py", line 321, in resolve sub_match = pattern.resolve(new_path) File "C:\Python27\lib\site-packages\django\core\urlresolvers.py", line 221, in resolve kwargs.update(self.default_args) TypeError: 'function' object is not iterable [08/May/2013 19:30:45] "GET /hello/ HTTP/1.1" 500 62490

+0

堆棧跟蹤? :) – karthikr 2013-05-08 18:58:34

+0

更新見上面 – ClaudioA 2013-05-08 19:03:52

+0

你能顯示urls.py嗎? – karthikr 2013-05-08 19:08:58

回答

0

變化:

url(r'^hello/$', spec_form, health_form)url(r'^hello/$', spec_form)

另外,在車型:

class Health_plan(models.Model): 
    name = models.CharField(max_length=15) 
    def __unicode__(self): 
     return "%s"%(self.name)