Sphene Community Tools


Copyright © 2007-2018 by Herbert Poul

You are not logged in.

Change Language:

AddThis Social Bookmark Button

A Django site.

Powered by Sphene Community Tools
Board » General » Overriding Model behavior

I have to say I am happier then ever about picking SCT as a starting base for my efforts. So far I'd say 80% of each modification I wanted to make is already there just not turned on by default.

Recently I wanted to change the behavior of one of the models in SCT. I wanted to enable administration editing of certain attributes and slightly adjust the default behavior. So I went poking around and figured out what I needed to do. Basically I needed to alter the editable flag and to change the setting for the default flag on a few attributes.

I did this, restarted the system and everything worked like I wanted.

This got me thinking, I made these changes to the actual SCT models directly. I would have liked to have been able to adjust a setting in my settings_local to achieve the same results. I searched the Django manual for how to do this. But it seems this is not something that is supported by any other means then what I did (change the original source, or start a project where I extend the model in my own model classes).

Lets take one concrete example. I wanted to enable editing the author of a post in the admin interface. So I altered the sphenecoll/sphene/sphboard/models.py and removed the 'editable = False' attribute flag for author.

Is there a better way to have done this?
well.. for this kind of customization .. i fear there is no easy way ..

i have tried it .. and my solution is decoupled from SCTs code base .. but.. really really ugly :)

i have created a simple django app (with just a models.py) and put it in the INSTALLED_APPS before the sphboard app .. and used the following code:

from django.db.models import signals
from django.dispatch import dispatcher

def class_prepared(sender):
    #print "class was prepared: %s" % repr(sender)
    if sender.__name__ == 'Post':
        for f in sender._meta.fields:
            if f.attname == 'author_id':
                f.editable = True
                print "author is now editable %s" % str(f.editable)

dispatcher.connect(class_prepared, signal=signals.class_prepared)

also comparing if sender._meta.app_label == 'sphbord' would make it a bit better

but.. i fear.. this is not really .. all that great ..

but i think most of the time this isn't really necessary because i think on "usual" installations it is quite clear what settings those fields should have.. because they are not really installation dependent IMHO..

(and .. in the end .. it should be the goal to have all of SCTs functionality in custom views, so that users don't have to use the django admin at all.. although .. there is still quite a bit missing for this goal)

--- Last Edited by Herbert Poul at 2008-06-14 07:46:29 ---
Hey, we have Signatures !!! Great, isn't it ? ;)

Please login to post a reply.

Powered by Sphene Community Tools