Getting Started — Django Categories v1.0b1 documentation

Django Categories v1.0b1 documentation

# Getting Started

- Table of Contents
- index
  next
  previous

# Getting Started

You can use Django Categories in two ways:

1. As storage for one tree of categories, using the `Category` model:

       Top Category 1
         Subcategory 1-1
           Subcategory 1-2
             subcategory 1-2-1
       Top Category 2
         Subcategory 2-1

2. As the basis for several custom categories (see Creating Custom Categories), e.g. a MusicGenre model

       MusicGenre 1
         MusicSubGenre 1-1
         MusicSubGenre 1-2
           MusicSubGenre 1-2-1
       MusicGenre 2
         MusicSubGenre 2-1

   and a Subject model

       Subject 1
         Discipline 1-1
         Discipline 1-2
           SubDiscipline 1-2-1
       Subject 2
         Discipline 2-1

## Connecting your model with Django-Categories

There are two ways to add Category fields to an application. If you are in control of the code (it’s your application) or you are willing to take control of the code (fork someone else’s app) you can make a Hard Coded Connection.

For 3rd-party apps or even your own apps that you don’t wish to add Django-Categories as a dependency, you can configure a Lazy Connection.

### Hard Coded Connection

Hard coded connections are done in the exact same way you handle any other foreign key or many-to-many relations to a model.

    from django.db import models

    class MyModel(models.Model):
        name = models.CharField(max_length=100)
        category = models.ForeignKey('categories.Category')

Don’t forget to add the field or fields to your `ModelAdmin` class as well.

### Lazy Connection

Lazy connections are done through configuring Django Categories in the project’s `settings.py` file. When the project starts up, the configured fields are dynamically added to the configured models and admin.

If you do this before you have created the database (before you ran `manage.py syncdb`), the fields will also be in the tables. If you do this after you have already created all the tables, you can run `manage.py add_category_fields` to create the fields (this requires Django South to be installed).

You add a many-to-one or many-to-many relationship with Django Categories using the FK_REGISTRY and M2M_REGISTRY settings respectively. For more information see Registering Models.