Archive for December, 2006

No more phone lines… almost there

Sunday, December 17th, 2006

As a software/data guy I always dream of abolishing physical boundaries so everything can be in software, virtualized. Besides a computer and a place to sit, what do you need to work?

(more…)

Using Django admin’s search in your own views

Friday, December 8th, 2006

In liu of a real fulltext search engine for Django, you may just want to use the same simple search engine that Django’s admin uses. Unfortunately it’s not readily reusable, so you’ll have to pull it out of the ChangeList.getqueryset in django.contrib.admin.views.main.


  def django_admin_keyword_search(model, keywords):
      """Search according to fields defined in Admin’s search_fields"""
      if not keywords: return []
      fields = model._meta.admin.search_fields

      qs = QuerySet(model)
      for keyword in keywords:
          or_queries = [ Q(**{‘%s__icontains’ % field: keyword}) for field in fields ]
          other_qs = QuerySet(model)
          if qs._select_related:
              other_qs = other_qs.select_related()
          other_qs = other_qs.filter(reduce(operator.or_, or_queries))
          qs = qs & other_qs

      return qs
 

Alternate syntax for Django urlpatterns

Sunday, December 3rd, 2006

Django’s urlpatterns variable has traditionally expected callbacks to be specified as strings, and extra arguments (commonly used with generic views) to be passed as a separate dict, for example:


  info_dict = {
    ‘queryset’: Entry.objects.all(),
    ‘date_field’: ‘pub_date’,
  }

  urlpatterns = patterns(‘django.views.generic.date_based’,
    (r‘^(?P<year>\d{4})/$’,   ‘archive_year’,   info_dict),
  )
 

(more…)


tracker