This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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() | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
NewerOlder