- Author:
- codigodaniel
- Posted:
- January 5, 2011
- Language:
- Python
- Version:
- 1.2
- Score:
- 0 (after 2 ratings)
Just extends your models from this One... is abstract so, it will not generate a table.
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)
then you can create the templates, and get the urls like this:
{{ object.get_absolute_url }} View
{{ object.get_update_url }} Edit
{{ object.get_delete_url }} Delete
{{ dummy_object.get_create_url }} Create
dummy_object is a quick and dirty solution until I find some better...
With all these you can create 54 functional and low detail CRUDS in one hour. :D
Enjoy!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | 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<object_id>\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<page>[0-9|last]+)?$" % (self._meta.module_name)
def get_delete_url_regexp(self):
return "^%s/(?P<object_id>\d+)/delete$" % (self._meta.module_name)
def get_update_url_regexp(self):
return "^%s/(?P<object_id>\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<page>[0-9]+)?$
^animals/ ^dog/(?P<object_id>\d+)/$
^animals/ ^new/dog/$
^animals/ ^dog/(?P<object_id>\d+)/update$
^animals/ ^dog/(?P<object_id>\d+)/delete$
^animals/ ^cat/(page/)?(?P<page>[0-9]+)?$
^animals/ ^cat/(?P<object_id>\d+)/$
^animals/ ^new/cat/$
^animals/ ^cat/(?P<object_id>\d+)/update$
^animals/ ^cat/(?P<object_id>\d+)/delete$
^animals/ ^bird/(page/)?(?P<page>[0-9]+)?$
^animals/ ^bird/(?P<object_id>\d+)/$
^animals/ ^new/bird/$
^animals/ ^bird/(?P<object_id>\d+)/update$
^animals/ ^bird/(?P<object_id>\d+)/delete$
'''
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 8 months ago
Comments
Please login first before commenting.