Robert Lujo's Homepage

Mar 22

#Django-haystack - #ElasticSearch (and probably #Solr) - how to filter/exclude by None/Null values value

If you are using django-haystack, you may notice that filter by None won’t work:

from haystack.query import SearchQuerySet

Haystack won’t report any error and will return no results.

Workaround for this I found by using Raw input field type value. So, solution follows:

from haystack.inputs import Raw
SearchQuerySet().exclude(some_field=Raw("[* TO *]")})

Other way around, filter all documents that have non-emtpy field value:

from haystack.inputs import Raw
SearchQuerySet().filter(some_field=Raw("[* TO *]")})

This could work for SOLR too and for other Haystack backends one can apply the same concept but with specific syntax of backend search language.

This solution is posted to StackOverflow where one can find more details:

Mar 07

#Django forms (#python) - prepare field values (e.g. .strip()) of all input fields before validation

I have a form that contains forms.URLField:

class MyForm(ModelForm):
    long_url       = forms.URLField(label=_(u"URL"       ), max_length=300, required=True,)

When a user inputs value that has trailing spaces, default URLField validation fails: Enter a valid URL.

I haven’t found some standardized and elegant way how to allow this, and looking into the source of django.form and I found several ways how to workaround this.

Chose inheriting Form._clean_fields method and changing before validation occurs. is immutable QueryDict instance, so I needed to override this.


def _clean_fields(self):
    if "long_url" in
        orig = = True["long_url"] =["long_url"].strip() = orig
    return super(MyForm, self)._clean_fields()

This could be generalized, i.e. how to prepare field values for all input fields before validation. Example, remove leading and trailing blanks from all string fields. Create a custom base ModelForm class which needs to be inherited. This implementation is more robust, since it supports MergedDict object too (dict attribute contains list of QueryDicts which are merged):

class MyModelForm(ModelForm):

    def _clean_fields(self):
        for qdict in getattr(, "dicts", []):
            orig = qdict._mutable 
            qdict._mutable = True
            for k, v in qdict.iteritems():
                if isinstance(v, basestring):
                    qdict[k] = v.strip()
            qdict._mutable = orig
        return super(MyModelForm, self)._clean_fields()

class ShortenUrlNewForm(MyModelForm):

    long_url       = forms.URLField(label=_(u"URL"       ), max_length=300, required=True,)

This can be applied for normal Forms too.

Jan 18

Displaying the content of #django {% debug %} tag

django has one interesting tag:


Outputs a whole load of debugging information, including the current context and imported modules.

I use this boilerplate to display it properly:

<pre> {% filter force_escape %} {% debug %} {% endfilter %} </pre>

Other debug template techniques I posted on stackoverflow.

Dec 05

Simulation of custom domains/subdomains on localhost

edit hosts:

sudo vim /etc/hosts

and add:       somesite.zz       robert.somesite.zz       lujo-lujo.zz       robert.lujo-lujo.zz

test it:

- start local dev server # in my case django app
- open browser: robert.somesite.zz

Oct 24

OS X keyboard shortcut to change focus to next/previous window

Usually OS X application can have multiple active windows. One can see the list of opened windows in application Window menu bar, and change the focus by selecting one of them. I was searching for some OS X keyboard shortcut to do this without mouse. There are several ways to do this (, but I find these two the most convenient:

The important thing is that the cycling is done through all windows of all opened applications - like a “better” alt-tab.

Sep 08

Algorithm: Sum digits of all whole numbers to nr N

Recently I needed to solve algorithm problem “sum digits of all whole numbers to nr N”. I wanted to find out solution that scales, i.e. that doesn’t iterate through all numbers. After playing around with algorithm, I found it out.

Read More

Jul 19

Clojure and 4clojure - reached top list 200

In my spare time I’m learning clojure.

Haven’t done any serious stuff (yet), but I have been solving problems on I was pretty satisfied how it went, and my final goal was to reach top list 200.

Read More

May 12

Answering questions on Stackoverflow

The last few days I was answering questions on stackoverflow - mainly related to python and db2. It came out a bit addictive. Now I have 20 answers, raised my reputation to 449 and reached top 10% this week :).

Check my profile at .

Update: The addiction needed to stop. Resume: 51 answers, reputation > 1200. It was fun :)

Jan 18

Destructuring assignment / tuple unpack in Python - by examples

Destructuring assignment (a.k.a. tuple unpack) in Python, seems to be very useful thing. The most basic variant:

»> x,y = 1,2
»> x,y
(1, 2)

what is identical to:

»> x, y = [1,2]
»> x, y = (1,2)

Read More

Dec 15

permset - utility for *nix permissions mgmt based on patterns - release 0.22

Permset is simple standalone utility script to manage *nix permissions on file and directory trees based on patterns. New release 0.22 contains following improvements:

Dec 11

permset - utility for mgnt of *nix permissions based on patterns

I had just released 0.20 version of permset.

Permset is simple standalone utility script to manage *nix permissions on file and directory trees based on patterns.

Check the example session to get into the idea behind.

Sep 24

Twitter incremental backup in YAML format - python, HTML get and parse

I use Twitter primarly as a bookmarking service.

I know that Twitter is not bookmarking service and there are specialized services for such needs. In my case, using this way started as an experiment, but later I found this way very convenient.

I wrote a script that enables me to incrementally backup all my tweets in a textual file in YAML format, by parsing HTML Twitter pages. Explanation and the script follows …

Read More

Sep 08

Mercurial branches - ‘named branches’ way - prefered way to do it?

Mercurial provides many ways to do branch based development. I was searchingfor the best in-repo branch solution. I chose named branches …

Read More

Jan 09

How to count characters, bytes and words in Vim

The article provides some information and tips on the subject: how to count characters, bytes and words in Vim.

Read More

Jan 05

Go on windows tutorial (#golang)

Recently I invested some time to learn about new and popular language Go. I wanna give a test-drive, and since I have Windows XP and there is no GO official windows Go port, I needed to do some manual work.

Read More