python - newbie Django smart/chained menu trouble -
i trying make chained select menu, have model:
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
and forms.py
:
class specform(modelform): = doctors_list.objects.values_list('specialty', flat=true) unique = [('---------------','---------------')] + [(i,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 )
my urls.py
:
from django.conf.urls import patterns, include, url testapp.views import spec_form testapp.views import health_form 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), url(r'^hello/$', health_form), )
and views.py
:
from django.shortcuts import render_to_response testapp.forms import specform 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})
i new django , find tricky. user must select 1 specialty, , should appear health_plans cover specialty.
the health_plans have manytomany relationship doctors. when person chooses specialty, script should check wich doctors belong specialty , retrieve health plans hold doctors.
so far thing in menu is: health_plan object health_plan object health_plan object
i hope made clear, code isn't.
any kindly appreciated
this has nothing chained selects, , of code here irrelevant. issue that, while doctors_list
has __unicode__
method, health_plan
not. define 1 model too.
(also note usual style model names capwords: doctorslist
, healthplan
. although former refers single doctor, should doctor
.)
Comments
Post a Comment