Hi Malcolm Thanks for reply.
whatever you said i do agree. I do not have huge dataset. whatevere i have given the records in table (See earlier message of this discussion) that is fixed not going to be expand. as you said i wrote Level3 inside Level2 def latest_listing(request): listing_result = Listing_channels.objects.all().filter (list_channel='Sight Seeing') for n in listing_result: print "N1.id : ", n.id if n.list_channel: qset=( Q(list_parent_id = n.id) ) level2 = Listing_channels.objects.filter(qset).distinct() for n in level2: print "N2.id : ", n.id if n.list_channel: qset=( Q(list_parent_id = n.id) ) print "QSET : ", qset level3 = Listing_channels.objects.filter(qset).distinct () for n in level3: print "N3.id : ", n.id if n.list_channel: qset=(Q(list_parent_id = n.id)) level4 = Listing_channels.objects.filter(qset).distinct() #sight_result = Listing_channels.objects.all().filter (list_parent_id=6) print "Listing Result : ", listing_result print "Level2 : ", level2 print "Level3 : ", level3 print "Level4 : ", level4 #print "Sight Result : ", sight_result #return render_to_response('sight_seeing.html', {'listing_result':listing_result,'level2':level2,'level3':level3}) return render_to_response('sight_seeing.html', {'listing_result':listing_result}) But even though my 'level3' list is empty. here is my output N1.id : 2 N2.id : 6 QSET : (AND: ('list_parent_id', 6L)) N3.id : 12 N3.id : 13 N3.id : 14 N2.id : 7 QSET : (AND: ('list_parent_id', 7L)) N2.id : 8 QSET : (AND: ('list_parent_id', 8L)) N2.id : 9 QSET : (AND: ('list_parent_id', 9L)) N2.id : 10 QSET : (AND: ('list_parent_id', 10L)) N2.id : 11 QSET : (AND: ('list_parent_id', 11L)) Listing Result : [<Listing_channels: Sight Seeing>] Level2 : [<Listing_channels: Activities>, <Listing_channels: Amusement park>, <Listing_channels: Art Galleries>, <Listing_channels: Museums>, <Listing_channels: Parks>, <Listing_channels: Tour operators>] Level3 : [] Level4 : [] I tried in other way too def latest_listing(request): channels = Listing_channels.objects.all().filter(parent_id=0) res={} print channels for channel in channels: subchannels = getsubchannels(channel.id) if subchannels: for subchannel in subchannels: #res[subchannel.list_channel]=[] categorys = getsubcategory(subchannel.id) if categorys: result=[] for category in categorys: result.append(category.list_channel) res[subchannel.list_channel]=result print res return render_to_response("Restaurants.html", {'channels':channels,'result':result,'res':res}) def getsubchannels(channel): channels = Listing_channels.objects.all() for n in channels: if n.list_channel: qset = ( Q(parent_id = channel) ) subchannels = Listing_channels.objects.filter(qset).distinct() return subchannels def getsubcategory(channel): channels = Listing_channels.objects.all() for n in channels: if n.list_channel: qset = ( Q(parent_id = channel) ) subchannels = Listing_channels.objects.filter(qset).distinct() return subchannels On Jan 21, 2:26 pm, Malcolm Tredinnick <malc...@pointy-stick.com> wrote: > On Wed, 2009-01-21 at 00:43 -0800, Praveen wrote: > > class Listing_channels(models.Model): > > list_parent_id = models.IntegerField(blank = True) > > list_channel = models.CharField(max_length = 20) > > visibility = models.BooleanField() > > index = djangosearch.ModelIndex(text=['list_channel'], additional= > > []) > > > def __unicode__(self): > > return self.list_channel > > > def get_absolute_url(self): > > return u'%s' % self.list_channel > > > these are the records of list_channel > > > id list_parent_id list_channel > > 1 0 Top Menu > > 2 1 Sight Seeing > > 3 1 Real Estate > > 4 1 Shopping > > 5 1 Restaurants > > 6 2 Activities > > 7 2 Amusement park 1 > > 8 2 Art Galleries > > 9 2 Museums > > 10 2 Parks > > 11 2 Tour operators > > 12 6 Bowling > > 13 6 Dinner Cruises > > 14 6 Diving > > 15 3 Developers > > 16 3 Orientation > > 17 3 Real Estate Agents > > 18 3 Relocation > > > I wanted to print like this for Sight Seeing see here > >http://explocity.in/Dubai/sightseeing/activites/diving-sightseeing.html > > > i wrote in views.py > > > def latest_listing(request): > > listing_result = Listing_channels.objects.all().filter > > (list_channel='Sight Seeing') > > for n in listing_result: > > print "N1.id : ", n.id > > if n.list_channel: > > Are you mixing tabs and spaces in your source code? Because this isn't > legal Python syntax. The "if..." line must be on the same indentation > level as the "print" line. > > > qset=( > > Q(list_parent_id = n.id) > > ) > > level2 = Listing_channels.objects.filter(qset).distinct() > > for n in level2: > > print "N2.id : ", n.id > > if n.list_channel: > > qset=( > > Q(list_parent_id = n.id) > > ) > > print "QSET : ", qset > > level3 = Listing_channels.objects.filter(qset).distinct() > > This won't be doing what you expect. You are creating a new qset every > time around the "level2" loop, but then the level3 queryset only uses > the last "qset" value. Maybe you want to create a level3 queryset for > every pass around the level2 loop? So you need to nest the loops, so > that the level3 loop is run inside the level2 loop. > > > for n in level3: > > print "N3.id : ", n.id > > if n.list_channel: > > qset=( > > Q(list_parent_id = n.id) > > ) > > level4 = Listing_channels.objects.filter(qset).distinct() > > #sight_result = Listing_channels.objects.all().filter > > (list_parent_id=6) > > > my query is how may i get the value for Level3. > > > Please do not suggest me to use django-tree or mptt. > > For large data sets, they, or some variation on them, are the right sort > of solution to this problem. For shallow trees of a fixed depth and not > too many nodes, what you're doing will work. If you're planning to scale > up to a few hundred items, however, you'll need a better plan than your > current one, since you're making essentially one database query per node > in the tree (which looks like being one node per object). That's a lot > of database queries. > > For a reasonable, but not huge data set (say, a few hundred up to a > couple of thousand items), you might want to consider pulling them all > back into Python in one database query > (ListingObjects.objects.filter(...)) and then organising the data > structure in Python by examining the parent_id values and looping over > the list. > > Regards, > Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---