Coalesce for F()

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from django.db.models.expressions import F

def __floordiv__(self, other):
    return self._combine(other, '||', False)
F.__floordiv__ = __floordiv__

class CF(F):
    """
    A coalesced expression representing the value of the given field.
    """

    def __init__(self, name, default=''):
        super(CF, self).__init__(name)
        self.default = default

    def evaluate(self, *args, **kwargs):
        res = super(CF, self).evaluate(*args, **kwargs)
        return 'COALESCE(%s, %%s)' % res[0], res[1] + (self.default,)

More like this

  1. Improved Pickled Object Field by taavi223 3 years, 9 months ago
  2. Pull ID from arbitrary sequence by nirvdrum 5 years, 10 months ago
  3. Logging Middleware by Magus 5 years, 7 months ago
  4. Add a button to the top of the admin change form by tsaylor 3 years, 2 months ago
  5. Bulk Insert - updated 5/9/2008 by coolie 5 years, 7 months ago

Comments

(Forgotten your password?)