I'm struggling to understand the db api when it comes to nested
relationships, I have three tables 'Channel', 'Category', 'Product'.
Each channel can have one or many categories, and each category can
have 1 or many products, also each product can be listed in 1 or many
categories (so many to many).
What i'm trying to do is select all products that are in a certain
channel, via the categories.
I can get as far as creating the category_set, from the channel
instance, as documented in the api docs, but I get lost when trying to
get all of the resulting querysets from the category_set.
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 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
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---