Showing posts with label inheritance. Show all posts
Showing posts with label inheritance. Show all posts
Tuesday, January 26, 2010
Django model field inheritance
To inherit model field in Django is not enough simply inherit a field class. It is needed to add attribute __metaclass__ = models.SubfieldBase. After that, you can overload any method you want.
Labels:
django,
inheritance
Saturday, September 26, 2009
Use objects as a django view functions
To use objects instead of simple functions I apply the following method:
class Page(object): # Variables template = "main.html" vars = {} redirect_url = "" def __call__(self,request,*args,**kwargs): # Before calling a "main" content-generating method self.before_content(request,*args,**kwargs) # "Main" content-generating method. Must be declared in descendants self.content(request,*args,**kwargs) # After generating content self.after_content(request,*args,**kwargs) # if need to redirect - do it if self.redirect_url: # if is present special redirect-field in GET url = request.GET.get(REDIRECT_FIELD_NAME) return HttpResponseRedirect(url if url else self.redirect_url) # Just return early declared template return render_to_response(self.template, context_instance=RequestContext(request, self.vars)) def before_content(self,request,*args,**kwargs): pass def after_content(self,request,*args,**kwargs): pass class PageAuth(Page): def __call__(self,request,*args,**kwargs): if request.user.is_authenticated(): return super(PageAuth,self).__call__(request,*args,**kwargs) return HttpResponseRedirect("{0}?{1}={2}".format(reverse(settings.LOGIN_VIEW),REDIRECT_FIELD_NAME,request.path)) class MainView(Page): def content(self,request,*args,**kwargs): self.template = 'template_name.html' self.vars['list'] = []
And has been changed url declarations. An example:
url(r'^$', MainView(), name="main_page")
(I couldn't find decision without using name parameter).
Labels:
django,
inheritance
Subscribe to:
Posts (Atom)