Skip to content

Instantly share code, notes, and snippets.

View igorsobreira's full-sized avatar

Igor Sobreira igorsobreira

View GitHub Profile
@igorsobreira
igorsobreira / gist:286051
Created January 25, 2010 17:32
wondering how dynamic fields could be added to a model...
# wondering how dynamic fields could be added to a model...
import copy
from django.db import models
class DynamicModel(models.base.ModelBase):
def _prepare(cls):
# calling a function or method to return the extra fields to
# add to the model
@igorsobreira
igorsobreira / formatters.py
Created January 29, 2010 02:40
Django form field for Brazilian Real (R$)
from django.template.defaultfilters import floatformat
from django.contrib.humanize.templatetags.humanize import intcomma
from django.utils.encoding import force_unicode
def decimal_to_real(value, precision=2):
'''
Receives a Decimal instance and returns a string formatted as brazilian Real currency:
12,234.00. Without the "R$".
'''
value = floatformat(value, precision)
@igorsobreira
igorsobreira / gist:323001
Created March 5, 2010 18:38
An way to add methods to an instance in Python
'''
An way to add methods to an instance.
>>> class Person(object):
... def __init__(self, name):
... self.name = name
...
>>> def upper_name(self):
... return self.name.upper()
...
from django.shortcuts import render_to_response
def myview(request):
...
return render_to_response('template.html', {'foo': ['one', 'two']})
from django.shortcuts import render_to_response
from django.template import RequestContext
def myview(request):
...
return render_to_response('template.html',
{'foo': ['one', 'two']},
context_instance=RequestContext(request))
from django.views.generic.simple import direct_to_template
def myview(request):
...
return direct_to_template(request, 'template.html',
{'foo': ['one', 'two']})
from settings import *
DEBUG = True
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'db.sqlite',
'USER': '',
'PASSWORD': '',
'HOST': '',
try:
from local_settings import *
expect ImportError:
pass
DEBUG = True
INTERNAL_IPS = ('127.0.0.1',)
MIDDLWARE_CLASSES = MIDDLWARE_CLASSES + ('debug_toolbar.middleware.DebugToolbarMiddleware',)
INSTALLED_APPS = INSTALLED_APPS + ('debug_toolbar',)
import os
PROJECT_ROOT = os.path.dirname(__file__)
try:
execfile(os.path.join(PROJECT_ROOT, 'local_settings.py'), globals(), locals())
except IOError:
pass