Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to get children of a category #80

Open
mayuroks opened this issue Nov 25, 2013 · 2 comments
Open

Unable to get children of a category #80

mayuroks opened this issue Nov 25, 2013 · 2 comments

Comments

@mayuroks
Copy link

I am using django-categories to implement music related app. I want artist as my category and his/her songs as children

models.py

from django.db import models
from django_extensions.db.fields import AutoSlugField
from categories.models import CategoryBase

class Artist(CategoryBase):
cat = models.CharField(max_length=255, blank=True)

def __unicode__(self):
    return self.name

class Song(models.Model):
title = models.CharField(max_length=255,)
slug = AutoSlugField(populate_from='title', unique=True)
description = models.TextField()
cat = models.ForeignKey(Artist, blank=True)

def __unicode__(self):
    return self.title

In templates, artist_details.html

{% extends 'base_post.html' %}
{% load category_tags %}
{% block page_content %}

{{ artist.name }}

{% if artist.children.count %}

Subcategories



    {% for child in artist.children.all %}
  • {{ child }}

  • {% endfor %}

{% endif %}

The template is getting rendered coz I can see artist's name. But i am unable to fetch the children. I checked the docs but I could not find much stuff related to fetching children.

There is data for both models in my DB, I added relevant info via admin interface. Can anyone tell me what I am missing ?

@coordt
Copy link
Contributor

coordt commented Nov 25, 2013

I think you've planned your schema out incorrectly. From the looks of it, you really don't need to have Artist subclass CategoryBase, you could just subclass models.Model.

You have a typical Many-to-One relationship and should be able to get the songs using artist.song_set.all

True children in Categories are the same model, not two different models. Does that answer your question, or did I misunderstand something?

@mayuroks
Copy link
Author

thankx .. totally got it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants