Python

Read file contents

import sys, os f = open(‘/path/to/file.xml’, ‘r’) contents = f.read() first_line = f.readline() second_line = f.readline() list_of_all_lines = f.readlines() f.close()   http://docs.python.org/tutorial/inputoutput.html  

Backing up your Django database

python

First dump all the data in a file: » python manage.py dumpdata > data.json or » python manage.py dumpdata > data.json –natural Now load the data to your new database: » python manage.py loaddata data.json

Django / Python Query with variable number of arguments

python

Put all conditions in a kwargs dictionary: kwargs = {} kwargs[‘price__gt’] = 20 kwargs[‘category’] = ‘televisions’ kwargs[‘language’] = ‘NL/nl’ records = Product.objects.all().filter(**kwargs).select_related().order_by(‘order’) Does the following: Select all instances of de Product-model with a price higher then 20, in the category ‘televisions’ and language ‘NL/nl’.