Benbob
Benbob

Reputation: 14264

Django modeling problem, need a subset of foreign key field

I intend to create an app for categories which will have separate category sets (vocabularies) for pages, gallery, product types etc. So there will need to be two models, vocabulary and category.

The categories/models.py code might be something like this:

class Vocabulary(models.Model):
    title = models.CharField()

class Category(models.Model):
    title = models.CharField()
    vocabulary = models.ForeignKey(Vocabulary)

From my pages, blogs, gallery, etc apps how I will need a ForeignKey field to categories:

class Page(models.Model):
    title = models.CharField()
    content = models.TextField()
    category = models.ForeignKey('categories.Category')

This will of course list all available categories in the admin app. If I have a product I want only the product categories to be avaialble. How can I filter the available categories to a specific vocabulary?

I'm learning Django and not really sure where to begin. Maybe I have the whole model wrong? If there are any apps which already do it please let me know.

Upvotes: 4

Views: 1621

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799180

Filtering of selection like this is done in the form using a queryset, or in the admin interface with limit_choices_to.

Upvotes: 9

Related Questions