python - How to design hierarchy (tree) table to display Drop-down menu? -


i have table name layer
has 4 field : layer1,layer2,layer3,layer4

the model

class layer(models.model):     layer1 = models.charfield(max_length=255)     layer2 = models.charfield(max_length=255)     layer3 = models.charfield(max_length=255)     layer4 = models.charfield(max_length=255) 

because it's not standard format
can city/building/floor/department or city/floor/department
depend on user,only rules @ 4 layer

for example:

layer 1 : city  layer 2 : building layer 3 : 12 f   layer 1 : city  layer 2 : building layer 3 : 10 f layer 4 : department   layer 1 : city b layer 2 : 3 f  layer 1 : city b layer 2 : 3 f layer 3 : department  

and after setting need present drop-down related menu on web page enter link description here

my thought first query ,the user can choose layer1

layer1 = layer(q~(layer1=''),layer2='',layer3='',layer4='')  

if choose city a ,then

layer2 = layer(layer1='city a',q(layer2=''),layer3='',layer4='')  

if choose building a,then

layer3 = layer2.filter(layer2='building a',q(layer3=''),layer4='')   

and on

but seems not effectively. want ask directions how great???

an verbose explanation here tree model.

an imanigary model working with:

class nameoftabel(models.model):     """docstrings"""     parent_id = models.foreignkey('self', null=true, blank=true)  # 1.7 v. in 1.9 it's different     layer = models.charfield(max_length=255) 

before start, model supposed filled in admin area or via prepopulated scripts because "hard" set validations each layer if necessary.

the table has 3 columns:

id | parent_id | layer 

the first layer has have parent_id field empty.

id | parent_id | layer 1  |           | city 

in second layer, have assign "building a" parent "city a" , has #1 id.

id | parent_id | layer 2  | 1         | building 

in third , fourth layer setup same.

id | parent_id | layer 3  | 2         | 12 f 4  | 3         | department 

(if layers may empty alter charfield null=true.)

if above may why it's called tree. if picture this: (i added more rows)

city     building         12 f             department     building b         1 f             department b city b     building         2 f             department 

for displaying in "terminal" there must (left) joins in query , hope can (with many reputations points have).

layer1 | layer2     | layer3 | layer4 city | building | 12 f   | department city | building b | 1 f    | department b city b | building | 2 f    | department 

now how query table purpose in link you've provided:

  1. layer = namoftable.objects.filter(parent_id__isnull=true)
  2. layer = nameoftable.objects.filter(parent_id=id_from_ajax_call)
  3. layer = same #2
  4. layer = same #2

if @ closely see repeated codes, suggest load table 1 variable , querying #2, #3, #4 placed variable e.g. "child".

finally, setup in short way:

  • first of display on client side in first select box layer 1, others disabled
  • after choosen layer 1, send ajax displaying child , in sended data parent id
  • in views sended data parent id , query results, send back, display in select box layer 2 , allow (cancel disabled status)
  • for #3 , #4 layer same procedure previous point.

Comments