Django

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

Advertisements