You are probably getting this error because when you generate your list
from categories. you are adding sub categories as list. so your list will
be like following
[cat1,cat2,cat3,[subcat1,subcat2],[subcat3,subcat4]]
I think you want to use
list.extend(BuildList(category.childrens.all()))
so you will have a one flat list.
list.append(category) repeated twice itlookslike to me that you don't
actualy need an if statement here.
def BuildList(categories):
list = []
for category in categories:
list.append(category)
list.extend(BuildList(category.childrens.all()))
return list
this method would run same queries.
,
You have a for template tag in django . You can use it instead of your
custom filter.
Lastly please check this https://github.com/django-mptt/django-mptt
this is great for categories. this is good for keeping trees in relational
databases. when you query sub trees, it will do exactly one query every
time.
and there is a generic category app too if you need.
https://github.com/callowayproject/django-categories
I hope that helps.
On Thursday, June 14, 2012 9:44:50 AM UTC+3, Dominis wrote:
>
> Hello.
> I need a bit help with my code.
> I write a function for menu building, which should create a list of
> objects, for designing it in html code.
> Code of function below:
>
>> def BuildList(categories):
>> list = []
>> for category in categories:
>> if len(category.childrens.all()):
>> list.append(category)
>> list.append(BuildList(category.childrens.all()))
>> else :
>> list.append(category)
>> return list
>>
>
> I call this function for a list of categories, like this:
>
>> list = BuildList(Categories.objects.filter(parent = None))
>>
> My function work right, but in list i get not an objects, my values have
> only name of a categories. I thought it happens couse in my Category model
> in __unicode__ function i place a 'return self.name', and when i call my
> categories in function - it return just name. :(
> What i should do for get full objects in my list? With full collection of
> parameters.
> I suppose this question is easy, but i just start to learn python and
> django.
> Thx for you time and answers.
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/django-users/-/dDbJWAC6Xo8J.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.