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