from django.db import models from django.contrib.contenttypes.models import ContentType from django.core.urlresolvers import reverse class AutomagicModel(models.Model): class Meta: abstract=True def get_absolute_url(self): return reverse(self.get_absolute_url_alias(),kwargs={'object_id':self.id}) def get_create_url(self): return reverse(self.get_create_url_alias()) def get_index_url(self): return "/%s/%s/" % (self._meta.app_label,self._meta.module_name) def get_delete_url(self): return reverse(self.get_delete_url_alias(),kwargs={'object_id':self.id}) def get_update_url(self): return reverse(self.get_update_url_alias(),kwargs={'object_id':self.id}) def get_absolute_url_regexp(self): return "^%s/(?P\d+)/$" % (self._meta.module_name) def get_create_url_regexp(self): return '^new/%s/$' % (self._meta.module_name) def get_index_url_regexp(self): return "^%s/page/(?P[0-9|last]+)?$" % (self._meta.module_name) def get_delete_url_regexp(self): return "^%s/(?P\d+)/delete$" % (self._meta.module_name) def get_update_url_regexp(self): return "^%s/(?P\d+)/update$" % (self._meta.module_name) def get_absolute_url_alias(self): return "%s_%s_absolute" % (self._meta.app_label,self._meta.module_name) def get_create_url_alias(self): return '%s_%s_create' % (self._meta.app_label,self._meta.module_name) def get_index_url_alias(self): return "%s_%s_index" % (self._meta.app_label,self._meta.module_name) def get_delete_url_alias(self): return "%s_%s_delete" % (self._meta.app_label,self._meta.module_name) def get_update_url_alias(self): return "%s_%s_update" % (self._meta.app_label,self._meta.module_name) def build_generic_CRUD_urls(self, model_class): from django.conf.urls.defaults import url, patterns return patterns('django.views.generic', url(self.get_index_url_regexp(), 'list_detail.object_list', {'queryset': model_class.objects.all(),'paginate_by':2,'extra_context':{'dummy_object':self}},self.get_index_url_alias()), url(self.get_absolute_url_regexp(), 'list_detail.object_detail', {'queryset': model_class.objects.all()},self.get_absolute_url_alias()), url(self.get_create_url_regexp(), 'create_update.create_object', {'model':model_class},self.get_create_url_alias()), url(self.get_update_url_regexp(), 'create_update.update_object', {'model':model_class},self.get_update_url_alias()), url(self.get_delete_url_regexp(), 'create_update.delete_object', {'model':model_class,'post_delete_redirect':self.get_index_url()},self.get_delete_url_alias()), ) class Animal(AutomagicModel): content_type = models.ForeignKey(ContentType,editable=False,null=True) name = models.CharField(max_length=255) def __unicode__(self): return '('+str(self.id)+') '+self.name class Dog(Animal): is_lucky = models.BooleanField() class Cat(Animal): lives_left = models.IntegerField() class Bird(Animal): velocity = models.IntegerField() ''' In [1]: from animals.models import MyModel In [2]: m=MyModel() In [3]: m.id=3 In [4]: m.get_create_url() Out[4]: '/animals/new/mymodel/' In [5]: m.get_delete_url() Out[5]: '/animals/mymodel/3/delete' In [6]: m.get_index_url() Out[6]: '/animals/mymodel/' In [7]: m.get_update_url() Out[7]: '/animals/mymodel/3/update' and now with a child class In [1]: from animals.models import Dog In [2]: d=Dog() In [3]: d.id=4 In [4]: d.get d.get_absolute_url d.get_create_url d.get_delete_url d.get_index_url d.get_update_url In [4]: d.get_update_url() Out[4]: '/animals/dog/4/update' Now, in your urls.py do this: from django.conf.urls.defaults import * from animals.models import Dog, Cat, Bird urlpatterns = patterns('animals.views', url(r'^$', 'index', {},Dog._meta.app_label), ) dog=Dog() cat=Cat() bird=Bird() urlpatterns+=dog.build_generic_CRUD_urls(Dog) urlpatterns+=cat.build_generic_CRUD_urls(Cat) urlpatterns+=bird.build_generic_CRUD_urls(Bird) this are the generated urls ^animals/ ^dog/(page/)?(?P[0-9]+)?$ ^animals/ ^dog/(?P\d+)/$ ^animals/ ^new/dog/$ ^animals/ ^dog/(?P\d+)/update$ ^animals/ ^dog/(?P\d+)/delete$ ^animals/ ^cat/(page/)?(?P[0-9]+)?$ ^animals/ ^cat/(?P\d+)/$ ^animals/ ^new/cat/$ ^animals/ ^cat/(?P\d+)/update$ ^animals/ ^cat/(?P\d+)/delete$ ^animals/ ^bird/(page/)?(?P[0-9]+)?$ ^animals/ ^bird/(?P\d+)/$ ^animals/ ^new/bird/$ ^animals/ ^bird/(?P\d+)/update$ ^animals/ ^bird/(?P\d+)/delete$ '''