At 12:35 PM

#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:

At 11:43 AM

#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.

At 4:27 PM

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.

At 11:34 PM

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

At 5:42 PM

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

At 11:33 PM

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

At 10:02 AM

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:
  • variable substitution for user and group in patterns
  • short statistics displayed on the pattern listing
  • documentation improvements

At 6:34 PM

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.

At 7:35 PM

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