This is a very small, simple piece of code, but essential for using fields of type 'money' on MS SQL Server, through FreeTDS. This took me quite some time to hunt down, as get_placeholder() is in fact an undocumented feature.
Example:
class MyModel(models.Model):
price = MoneyField()
1 2 3 4 5 6 7 8 9 10 11 12 | from django.db import models
class MoneyField(models.DecimalField):
def __init__(self, *args, **kwargs):
# Default to the max size for SQL Server's money datatype
kwargs['max_digits'] = kwargs.get('max_digits', 20)
kwargs['decimal_places'] = kwargs.get('decimal_places', 2)
super(MoneyField, self).__init__(*args, **kwargs)
def get_placeholder(self, value):
return r'CONVERT(money, %s)'
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.