From 81d07c859d7291ea167b8c1d1f92d65a37c33e75 Mon Sep 17 00:00:00 2001 From: Corey Oordt Date: Mon, 5 Apr 2010 13:09:36 -0400 Subject: [PATCH] Added some docs and testing apps --- docs/registering_models.rst | 25 +++++++++++++++++++++---- sample/simpletext/admin.py | 7 ++++++- sample/simpletext/models.py | 6 +++++- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/docs/registering_models.rst b/docs/registering_models.rst index 027aacd..aa9fda6 100644 --- a/docs/registering_models.rst +++ b/docs/registering_models.rst @@ -17,8 +17,25 @@ categories.register_m2m(MyModel) If you want more than one field on a model you have to have some extra arguments -import categories -categories.register_fk(MyModel, 'primary_category') -categories.register_fk(MyModel, 'secondary_category', {'related_name':'mymodel_sec_set'}) + import categories + categories.register_fk(MyModel, 'primary_category') + categories.register_fk(MyModel, 'secondary_category', {'related_name':'mymodel_sec_set'}) -The ``extra_args`` allows you to specify the related_name of one of the fields so it doesn't clash. \ No newline at end of file +The ``extra_args`` allows you to specify the related_name of one of the fields so it doesn't clash. + + +Extra fields with Many-to-Many Categories +========================================= + +Occasionally you may want to add one or more fields to the categories you relate to a given model. For example, you may want an easy way to select one of the categories to be "primary." This requires a bit more work. + +1. Create the intermediary class:: + + from categories.models import CategoryIntermediary + + # We're assuming that there is a ``Blog`` model previously defined + # or imported + class BlogCategories(CategoryIntermediary): + blog = models.ForeignKey(Blog) + + categories.register_m2m(Blog, extra_args={'through':'BlogCategories'}) \ No newline at end of file diff --git a/sample/simpletext/admin.py b/sample/simpletext/admin.py index e36aadb..36a6790 100644 --- a/sample/simpletext/admin.py +++ b/sample/simpletext/admin.py @@ -1,4 +1,9 @@ from models import SimpleText from django.contrib import admin -admin.site.register(SimpleText) \ No newline at end of file + +class SimpleTextAdmin(admin.ModelAdmin): + filter_horizontal = ['cats',] + + +admin.site.register(SimpleText, SimpleTextAdmin) \ No newline at end of file diff --git a/sample/simpletext/models.py b/sample/simpletext/models.py index 7b3d6a0..030aaab 100755 --- a/sample/simpletext/models.py +++ b/sample/simpletext/models.py @@ -27,5 +27,9 @@ class SimpleText(models.Model): return ('simpletext_detail_view_name', [str(self.id)]) import categories +from categories.models import CategoryIntermediary +class SimpleTextCategories(CategoryIntermediary): + simpletext = models.ForeignKey(SimpleText) + categories.register_fk(SimpleText, 'primary_category', {'related_name':'simpletext_primary_set'}) -categories.register_m2m(SimpleText, 'cats') \ No newline at end of file +categories.register_m2m(SimpleText, 'cats', ) \ No newline at end of file