Login

django-pyodbc MoneyField

Author:
Tarken
Posted:
October 30, 2008
Language:
Python
Version:
1.0
Score:
0 (after 0 ratings)

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

  1. find even number by Rajeev529 1 month, 3 weeks ago
  2. Form field with fixed value by roam 2 months, 2 weeks ago
  3. New Snippet! by Antoliny0919 2 months, 3 weeks ago
  4. Add Toggle Switch Widget to Django Forms by OgliariNatan 5 months, 1 week ago
  5. get_object_or_none by azwdevops 9 months ago

Comments

Please login first before commenting.