I too am having trouble with querying across multiple tables.

my (simplified) models:-

class Channel(models.Model):#blog
        name = models.CharField("Name", maxlength=30)
        slug = models.SlugField("URL", prepopulate_from=('name',))

        class Admin:
                pass

        def __unicode__(self):
                return self.name


class Category(models.Model):
        name = models.CharField("Name", maxlength=100)
        slug = models.SlugField("URL", prepopulate_from=('name',))
        channel = models.ForeignKey(Channel, verbose_name="Channel")
        parent = models.ForeignKey("self", verbose_name="Parent Category",
blank='true', null='true', related_name="child_set")
        desc = models.CharField("Description", maxlength=500,blank='true')

        class Admin:
                pass

        def __unicode__(self):
                if self.parent:
                        prefix = str(self.parent)
                else:
                        return self.name

                return '>'.join((prefix,self.name))


class Prod(models.Model):
        title = models.CharField(maxlength=100)
        slug = models.SlugField("URL", prepopulate_from=("title",))
        precis = models.TextField(maxlength=100)
        body = models.TextField(maxlength=5000)
        categories = models.ManyToManyField(Category,
verbose_name="Category")

        class Admin:
                pass

        def __unicode__(self):
                return self.title

my view:-

def prods_by_channel(request, slug):

        try:
                ch = Channel.objects.get(slug__iexact=slug)
                ct = ch.category_set.all()
        except Category.DoesNotExist:
                raise Http404

        return list_detail.object_list(
                request,
                queryset = ct.prod_set.all(),
                template_name = "prods/prod_by_channel.html",
        )

I'm trying to write a query that will give all products from within a
given channel, I pass the slug into the view, and create a channel
instance, from which I get the 'category_set', so far so good, I know
the category_set contains further prod_sets, but I don't know how get
at them, using the view code above raises an attribute error... BTW,
my background is lowly frontend development, and I'm a relative
newbie, so please be gentle with me =0)

Could anyone point me in the right direction.....please


On Jan 11, 8:34 am, Malcolm Tredinnick <[EMAIL PROTECTED]>
wrote:
> On Wed, 2008-01-09 at 19:47 -0800, Ken wrote:
> > I need some advice.  I'm struggling with a query that spans 4 tables.
> > "Struggling" is a bad word; I'll explain later.  Django uses a model-
> > oriented query system together with a Foreign Key manager to get to
> > the "next" joining table.  A query that spans several tables results
> > in a deeplynestedloop.  
>
> No, it usually doesn't. Unfortunately, you haven't provided any specific
> details about what you are trying to do, so it's difficult to offer any
> advice. If you could post a short example illustrating the related
> models you've got and what you're trying to query, we can probably help
> out.
>
> Regards,
> Malcolm
>
> --
> The cost of feathers has risen; even down is 
> up!http://www.pointy-stick.com/blog/
--~--~---------~--~----~------------~-------~--~----~
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