Tips and Tricks

  • QuerySet: “or” filter – Q objects. (here)
  • Get specific columns: QuerySet.values() or QuerySet.values_list()
  • Get first and last elements of QuerySet (here)
  • String to dict (here)

    You can deserialize it using the built-in json module if you’re on Python 2.6+, otherwise you can use the excellent third-party simplejson module.

    import json    # or `import simplejson as json` if on Python < 2.6
    json_string = u'{ "id":"123456789", ... }'
    obj = json.loads(json_string)    # obj now contains a dict of the data
  • Filter by multiple values (here)
  • Add custom model methods
  • Using <select multiple>: request.POST.getlist(‘services’) and request.POST.get(‘service’) (here)
  • Serialize Python sets (here)
  • URL decoding/encoding

Official documentation

Other materials

Web hosting


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s