python - Modular architecture for both models and views in Django -


i want make foo app can work other apps in way other apps "inherit" models, views, , templates foo app.

set up

i'm making django app generic foos. 80% of time, generic foo (and related) models suffice store data, , other 20% of time, simple multi-table inheritance suffice extend generic models. however, 99% of time, generic views not suffice, , type of foo determine view use both foo , models related it. new foo types come existence every time new client, it's essential keep business logic in separate apps.[1]

note models related foo model, so:

from django.db import models class foo(models.model):   # ... model fields , generic business logic class bar(models.model):   foo = models.foreignkey(foo)   # ... other model fields , generic business logic class baz(models.model):   bar = models.foreignkey(bar)   # ... other model fields , generic business logic 

proposed solution

the foo model contain string name of app contains custom code describes data , behavior associated foo type. each such foo-type plug in have in models , views modules line from generic-foo.models import * or from generic-foo.views import *. include following code in generic foo code.

import sys import importlib django.db import models myproject import settings class foo(models.model):   foo_apps = [(app_name, app_name) app_name in               settings.installed_apps if 'foo' in app_name]   app_name = models.charfield(max_length=256, choices=foo_apps)   # ... other model fields , generic business logic   def get_app(self):     "return app right models or views object."     return importlib.import_module(self.app_name) 

then generic view gv uses foo instance (or instance of foo subclass) obj first call app = obj.get_app() , call app.views.gv(). class-based views can take care of rest of work. (remember common case view provided sub app anyway.) because of asterisk imports, can child foo (or related model) contenttype table running query contenttype.objects.get(app=obj.app_name, model=obj.__type__.__name__).

question

am doing hard way? (also, there security hole here i'm not seeing?)

related questions

[1] need able share code clients without revealing other clients' code.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -