Hi,
a while ago i have written a similar question:
http://groups.google.com/group/django-users/browse_thread/thread/b42239bcfb85e21f/0ec972719fbde390#0ec972719fbde390
and beside an extensible view for profiles i have now also added a few
extension mechanisms for my forum application (
http://sct.sphene.net)
- this allowed me to create a decoupled "link list" application which
is based on my forum - since i'm not really a python expert i would
love to get feedback if this is the best way to do it ..
there are basically four parts in this extension mechanism:
1. create a registry where applications can add their specific
extension classes (in my forum these are "Category Types")
2. add a hook to the post view to let the "category type" modify the
newforms-PostForm and add custom fields
3. add a hook to the post view to let the "category type" handle
saving of custom fields
4. add hooks to let the "category type" use different template files
a bit more description:
ad 1.) this is basically:
http://yourhell.com/wsvn/root/django/communitytools/trunk/sphenecoll/sphene/sphboard/categorytyperegistry.py
- all applications would then add their subclasses of 'CategoryType'
in their __init__.py file. the 'Category' model then has a
category_type field which contains the name of the CategoryType.
ad 2.) here is an example implementation:
http://yourhell.com/wsvn/root/django/communitytools/trunk/sphenecoll/sphene/sphlinklist/categorytype.py
which makes a linklist out of a forum category - the method
"get_post_form_class" would return a specific "LinkListPostForm" which
is extended from the forum's PostForm and simply adds a "link" field -
it also reorders the fields using:
self.fields.insert(1, 'link', self.fields['link'])
ad 4.) i simply have functions like "get_show_thread_template" which
would return the path and name of a template which should be
rendered.. this is usually extends from the default template and
overloads a few {% block %}'s
so .. my questions would be:
is this the right "django way" of allowing extensions for an
application ? is it useful to let applications add their classes from
an __init__.py file ? or would it be better to assume that every
extension would have a module named 'categorytype' in the
application's module ? ie. the forum would then iterate through all
applications to look for an 'categorytype' module ? would this be
better than to expect applications to register extensions themselves ?
- or simply using signals for extensions - as i've used for the
profiles:
http://sct.sphene.net/wiki/show/Community/UserProfiles/
and for newforms: is this a "valid" usage ? simply modifying the
"fields" object .. or would it be better to create a complete separate
form for the extension ? - i guess it impacts performance a bit
because i _always_ modify "fields" for each instance, on every request
instead of just modifying "base_fields" once (would this be somehow
possible ? i guess not ?)
it would be great if anyone could give me a hint if this is a good
approach or if it has some fundamental problems i haven't thought of.
thanks in advance & cu,
herbert
http://sct.sphene.net/