On 11 nov, 21:41, coderb <[EMAIL PROTECTED]> wrote:
> Hi All,
>
> I'm having trouble getting my head around how to set up my data entry
> and queries, so that a listing/detail entry is categorised properly.
>
> firstly, my models look like this:
>
> class Category(models.Model):
>     name          = models.CharField(max_length=100, unique=True)
>     slug          = models.SlugField(max_length=100, unique=True)
>     parent        = models.ForeignKey('self',blank=True,null=True)
>     ...etc..
>
> class Entry(models.Model):
>     categories    = models.ManyToManyField(Category)
>     title         = models.CharField(max_length=200)
>     description  = models.TextField()
>     ....etc..
>
> I'm trying to achieve this:
> Each entry can be linked to one or more categories
> When browsing/searching categories, I want all child entries (from
> next level down to lowest level) to be listed in one result.

I assume that what you want is to display all entries for a given
category *and* its children ?


> The manually selected categories from the list-box are obviously the
> only ones linked to the Entry, so when I someone browses/searches
> listings by category, they will not automatically get the all child
> Entries.
>
> my view filter to get Entries looks like this:
> entry_list=Entry.objects.filter(categories__slug=this_cat,....
>
> theoretically I can think of two options:
> 1. At the time an Entry is added/edited - I need to have a routine
> that rebuilds the many-many link table so that  the Entry is linked
> directly to each parent, of all selected categories.  Although this
> would cause other issues, like duplicated results on the search, how
> to display all linked categories on the detail page, changing the
> model to have a separate many-to-many link table  etc..

Wrong solution IMHO

> 2. The other option, sounds better to me, is the way the browse/search
> query is built, when a user is browsing a top level category, I need
> to somehow create a left/outer join or union so that all child
> categories are linked with their Entries.

Which is indeed the way to go.

> But since I do not know the
> depth of cateogories,  this sounds like quite a complex bit of code to
> dynamically build the sql, although it might be quite simple using
> some kind of custom method in the model or something.. (but would this
> query cause a performance issue.)

The "adjacency list" pattern you used for the Category model, while
the most obvious one, can indeed become somewhat costly when you want
to retrieve ancestors or childrens. Now there are other ways to handle
hierarchies in SQL. You may want to have a look here for other
options:

http://code.google.com/p/django-treebeard/

Anyway, a simple Q&D solution is to
1/ get a list of categories ids for the current category and it's
children (this should be a method of your Category model)
2/ filter your entries on this list, using the 'in' operator

This will at least reduce the number of queries on the Entry table.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to