Django – Insensitive Case

When using Django in Python there’s these awesome things called QuerySets that are a full blown ORM to make it so I don’t have to think in SQL to get things in and out of a database! One of the things I didn’t find documented very well was case sensitivity vs insensitivity.

If you’re using the MySQL backend the case-sensitive is implied, i.e. using BINARY LIKE instead of just LIKE

series = Series.objects.filter(name__contains=srch_str)

To make sure that you’re searching for some that’s case insensitive you can do the following:

series = Series.objects.filter(name__icontains=srch_str)

If you want it to work case insensitive no matter the backend, collation, or collection type just use icontain, istartswith, iendswith, iregex, iexact.

Django: Basic Template Tags

One of things I had the hardest time finding was a good example of Template Tags in Django; so here is my basic template tags file I add to every new project (GPLv2). A lot of people have their own, and I’m sure in the future I’ll add on to my basics; but for now these get me by. One of the things I’ve been wanting to do is write a good small primer for beginning Django, consider this my first foray in to making that a reality.

from django import template
from django.template import Variable, VariableDoesNotExist
register = template.Library()
 
@register.filter
def gVal(object, attr):
    """Check the array, dict, etc for a value"""
    quasai = { 'object' : object }
    #print "gVal :: " + str(attr) +" - "+ str(quasai)
    try:
        value = Variable('object.%s' % attr).resolve(quasai)
    except VariableDoesNotExist:
        value = None
    return value
 
@register.filter
def xVal(a,b):
    """Since Python resolves left to right in template tags: pass value and then the dick to check; returns value or 0"""
    #print "xVal",a,b
    if b.has_key(a):
        value = b[a]
    else:
        value = "0"
    return value
 
@register.filter
def hexVert(value):
    """Removes all values of # from the given string and replaces with %23"""
    if value != None:
        value = value.replace("#", '%23')
        value = value.replace("!", '%21')
        value = value.replace("&", '%26')
    return value

Every new beginning…

Greetings and welcome to this, the innumerable time I try to get myself to write more and keep to it. In the now over 15 years I’ve spent writing things for various website and incarnations of my own “blog” like platforms I’ve realized a few things; chief among them that when I first started writing I was beyond horrible, I’ve now creeped up to just horrible. I’ve built up a very large archive of drivel that I don’t really wish on anyone, though from time to time I may “republish” a post or story.

I’ve gone through several blog titles in the past: tks (the kitchen sink), peanut gallery, blog, musing in a half shell, etc. The original idea behind “tks” was to comment on the things I didn’t see other people talking about; everyone else talked about everything “except” the kitchen sink, or so the sang goes. I came to find that I didn’t always have time for such things and instead it turned in to random musings. This time I’ll keep the name for personal nostalgia, but with a very different purpose. This time the cliche of “write what you know” is more guiding my steps.

To be clear, this is not a New Year’s resolution of some kind, though it is coming at that time of year. My current job has been given me many days off during this holiday season, giving me time to catch up in other areas of my life and allow me the luxury of figuring out things I’d like to do going forward. To that end there are a few things I’d like to cover in my writings going forward:
Programing code & design
Trombone playing & philosophy
Gaming- digital, analog, design & implementation
Writing- musings on other and short stories of my own
Socio Political issues (not politics)
Religion
Physical Activity- my own and others
Cats

In the day and age of personal “branding” when looking for a job, or even when trying to be an authority on a subject, some of these topics/ideas may be inflammatory and damaging in some people’s eyes: oh well, my apologies. They might as well know now what my views are instead of three months in to a job that I’ve budgeted for six months, costing me said job.

For an overview post 432 words isn’t really all that many; and it’s easy to do on a day when there are no other real distractions. The real test is going to be int six months: am I still posting daily, weekly, semi-weekly, monthly, at all? Only time will tell.