Category Archives: django

Doing ajax with django models serialization: json part

Finally a technical post after a long while, here goes


On my new job, the site requires a crazy level of interactivity, probably for the wrong reason, but lets not go there now.

Along the way, we decide to use a mix of javascript, and html, with a help of json. Thats is where, django serialization comes in. What it does is, it serialize a django queryset into xml or json. If i need to use that to generate json for django queryset, because simplejson cannot do it, not directly.

To output a queryset to json is pretty easy.


from django.core import serializers
data = serializers.serialize(‘json’, queryset)


where queryset your djangoqueryset


in a sample database, in a called addressbook





to play around with the django shell, assume that data already there


>>> from django.core import serializers
>>> from addressbook.models import Contact
>>> serializers.serialize(‘json’, Contact.objects.all())
[{“pk”: 1, “model”: “addressbook.contacts”, “fields”: {“phone”: “012345678”, “post_code”: “68100”, “name”: “sweemeng”, “address”: “somewhere in selayang”}}]


To make it useful for my purpose to use in a javascript, i need to put this into a views, point your urls.py to the views, and run the server, and point your browser to it, you should able to see a json output




But here is the catch, one, this is strictly only for serializing django queryset, you can still use simplejson for other type of data. two it seems that django serializer don’t serialize a single model instance.


Next post, using it with a jquery

more information on django serialization



django, solr and a few interesting stuff Part 2 : Haystack….

****continuation from the last story******

As the Adventure Continue

One of the nice thing about django, it have all sort of application. Haystack is such a application. Not just haystack support solr, it also support a few other backends. This include whoosh, and xapian.

The fun part about this, it is quite pleasant to work with. So we redo all of out previous code to work with this.

Let The Game Begin

Again the tutorial is helpful, it pretty much describe what i am have done anyway. And the documentation covers a lot!! So I will skip the setup, and index.

After following the tutorial to setup.And modify the search_indexes.py for each models that we need to find. And generate the solr index, and copy to the solr/conf.

We basically have a django application ready to do search. To test, is a matter of going to shell.

from haystack.query import  SearchQuerySet
q = SearchQuerySet().all()

of course, you can also, do

q = SearchQuerySet().filter(fieldname=’value’)
for i in q:
      print i.object.value

It is similar to django queryset api. Except it is for search. You can do a lot more, like faceted search, chain the filter, etc.

Are we There Yet. 

Not quite, there yet. Because haystack also have a search page, which you can enable, also covered in the tutorial. You can inherit from the search view, and forms to fit your need.

An example is for a form that I need(it is generic anyway)

So you don”t even need to create your own form, and views.
Just import the views, and change the parameter

haystack is pretty comprehensive, have many stuff done for us, from views, forms that is functional. Doing indexing, generating index for solr, etc. I think this is a nice project to use in django for providing search in your web app

output json on django

I just finish a job in django, and I use it for some ajax part of the project. So I generate json for this. Below is a snippet sanitize from the code my job use…

try:
import json
except:
from simplejson as json
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response
def generate_json(request):
#it can be a dictionary too, anything supported by simplejson
somelist=[]
#do something here, to fill in somelist
encoded=json.dumps(somelist)
response[‘content-disposition’] = ‘attachment; filename=properties.json’
response=HttpResponse(encoded,mimetype=”application/json”)
return response

So there it is…

p.s I need a way to display source code properly on my blog, idea?

Fun with python: web development with Django: part 2, a little overview

Like many framework like, ruby on rails. Django have a set of utilities.

First thing first,
– django-admin.py. This is the main admin utilities, it have everything from start a project synchronize database, generate sql, shell, run the server.

usage:
just call it in shell
django-admin.py
this will show, the set of options and parameter

django-admin.py startproject name
this will start a project. Which will generate a directory, with the settings file, urls.py which map url to your application, __init__.py and manage.py

– manage.py. This is another admin utility, I didn’t see much difference detween this and manage.py. but in most django tutorial, we manage our project, with manage.py.
usage:
in the project directory, in shell
python manage.py
it will show a set of option, similar to django-admin.py

to create new application
python manage.py startapp application-name
this will create your application, the stub of models in model.py, views in view.py