views:

268

answers:

1

My DjangoApp is using categories to generate a navigation and to put stuff in those categories.

There are two types of categories:

  • ParentCategories (top categories)
  • ChildCategories (sub categories that have a ParentCategory as a parent)

Because those to categories are so similar I don't want to use two different models. This is my category model:

class Category(models.Model):
    name = models.CharField(max_length=60)
    slug = models.SlugField(max_length=80, blank=True)
    is_parent = models.BooleanField()
    parent = models.ForeignKey('self', null=True, blank=True)

In my djangoadmin the parent won't be represented. If I use python manage.py sql I get:

CREATE TABLE "catalog_category" (
    "id" integer NOT NULL PRIMARY KEY,
    "name" varchar(60) NOT NULL,
    "slug" varchar(80) NOT NULL,
    "is_parent" bool NOT NULL
)
;

So the parent relationship won't even be created.

Is there a handy way of fixing this?

I know I could just alter the table but I'm flushing/deleting the database quite a lot because the app changes rapidly and I don't want to alter the table everytime manually.

btw: my dev db is of course sqlite3. On the server we'll use postgresql

+1  A: 

Something else is going on - that definition of parent is fine. If I run manage.py sql on an app with that model copy-pasted in, I get:

BEGIN;
CREATE TABLE "bar_category" (
    "id" integer NOT NULL PRIMARY KEY,
    "name" varchar(60) NOT NULL,
    "slug" varchar(80) NOT NULL,
    "is_parent" bool NOT NULL,
    "parent_id" integer
)
;
COMMIT;
Dominic Rodger