views:

63

answers:

1

I'm in a situation where I've got a project that has a large number of Django views across quite a few different apps. The same codebase and database is being used by a large number of clients. There are a few site-specific use-cases that are coming in and this requires quite a bit of custom code to be written.

I'd like to come up with a strategy where we can have the default functionality and then replace django views based on user parameters and so on. Ideally I'd have a central codebase and a project or app per site but due to the current deployment strategy I don't see how this is possible without a massive refactor.

I don't like this idea but it might demonstrate the problem better, basically having some way to dynamically load up another module and replace the those in views.py would do the job. Or something like a function decorator that checked for a replacement for that function and it calls that or calls the default if it can't find it. Dynamic loading could be done with something like this

The problem I have here is the code base is really large and I need to retain the current API of course and maintain as much backwards compatibility as possible.

Basically I'm looking for suggestions for the most pythonic and clear way to do this.

+3  A: 

The most pythonic way would be to use classes for your views and then use inheritance to override specific parts of each view. In your description you describe something which sounds exactly like inheritance.

There are two relevant SO questions you might want to look at class views in django and django class view with decorator and sessions

Inheritance will give you the tools to do this fine grained overloading that you want to do I think.

This would of course require a bit of API change, but it is better to do it now than wait until the project is twice the size!

Nick Craig-Wood
Of course! *ping* I'd gotten my head into the views are views are functions state of mind and hadn't even considered classes. Thanks.
Orange Box