including site packages in virtualenv 1.7 Jan
13
1
2

So if you're using virtualenv 1.7 and are wondering why you can no loger see your global Python site-packages directory it's because the default behavior has changed.

Now the default install for virtualenv is to hide site packages; equivalent to passing the old --no-site-packages command:

virtualenv --no-site-packages <env>

Since this option is now default it's no longer needed, and the new command to see the global site packages is --system-site-packages:

vitualenv --system-site-packages <env>

Hopefully this saves you some confusion as it's not clear what's happening when your new install now suddenly is breaking.

comments

random integer with a range in python Dec
14
1
1

To create a random integer within a range you can use the following command:

import random

# random integer between 1 and 100
RANDOM_INTEGER = random.randint(1, 100)

comments

remove domain dns key entry in known_hosts for ssh Nov
22
1
1

Sometimes I re-key or move the domain of a server and need to reset the key so RSA doesn't complain. Technically you can just open ~/.ssh/known_hosts file and remove the entry for the dns, but the names are hashed which makes the entry difficult to find.

I used to just reset the entire known_hosts file, but that's not necessary. You can remove the domain entry with the simple command:

ssh-keygen -R example.com

Next time you try to ssh you'll be prompted to add the key as if you're visiting the site the first time. I hope this helps.

comments

showing a foreign key value in a django admin list display Nov
07
1
1

say you have two models like so:

# models.py

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.ForeignKey("Author")

class Author(models.Model):
    name = models.CharField(max_length=100)

In this case it would be handy if we could list the author in the list_display of the book admin, but Django gives an error if we list foreign keys like so:

# admin.py

class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'author__name')

admin.register(Book, BookAdmin)

Luckily we can easily fix this by adjusting how we link into the author foreign key. We just need to add a method to the Book ModelAdmin and return the desired string.

# admin.py

class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'author_name')

    def author_name(self, instance):
        return instance.author.name

Be careful though as gratuitous additions of foreign key field can greatly increase the load time of the admin page for the list view of the model: use this judiciously.

comments

ignoring files generated by eclipse for git Oct
18
1
1

If you're developing in Eclipse, like I am for Android, and you use version control you're going to want to ignore the default project files that are generated by Eclipse. You should update your .gitignore with the following list:

.project
.classpath
.settings/
bin/

comments

create/generate qr code with qrencode in ubuntu Oct
14
1
1

You can create qr codes easily in Ubuntu using qrencode. You can install it like so:

sudo apt-get install qrencode

and then create the qr code with the following command:

qrencode -o /path/to/qrcode.png -s 10 'http://example.com'

the `-s` option flag is the size of dot (pixel). The default is 3.

comments

how to rename a database in postgresql Oct
12
1
1

http://www.postgresonline.com/journal/archives/40-How-do-you-rename-a-database.html

First kick the users out of the database, you can find the users with the following command.

SELECT * 
    FROM pg_stat_activity 
    WHERE datname = 'myolddbname_goes_here'

now run the following:

ALTER DATABASE myolddbname_here RENAME TO mynewdbname_here

comments

manually writing to model filefield in django views Oct
11
1
1

Django models.FileField is very helpful in simplifying file management, but sometimes you need to write/overwrite into a file field object in a view without having the admin helper functions available. It's not inherently clear how to do this manually. Never fear though it's still an easy process.

It's worth noting that Django uses a custom File class that acts as a wrapper around the python file object to add some enhanced functionality. You simply need to initialize a Django File with a python file to get a valid model.

Say you have something similar to the following model:

class FileExample(models.Model):
    test = models.FileField(upload_to='uploads')

You can then write something like this in your view:

from django.core.files import File
from test.models import FileExample

file_example = FileExample()

filename = '/path/to/file.txt'
f = open(filename, 'r')
djangofile = File(f)
file_example.test.save('filename.txt', djangofile)
f.close()

Furthermore you can combine some statements to simplify and shorten the process:

file_example = FileExample()
file_example.test.save('filename.txt', File(open('/path/to/file.txt', 'r')))

comments

export a table into a csv file in postgresql Oct
05
1
1

I always have to look this up, so I'm placing this here. This exports a table into a csv file in PostgreSQL.

COPY (SELECT * FROM table) TO `/path/to/file.csv` WITH CSV HEADER;

comments

uploading multiple files with the same input name in django Sep
20
1
1

So sometimes I have an input field in a form that I want to upload files to but I want to upload multiple files at a time. I'll usually just use javascript to add and remove the field dynamically, and to keep things simple I'll keep the input name the same. I'll eventually end up with markup that looks similar to the following:

<input type="file" name="file" />
<input type="file" name="file" />
<input type="file" name="file" />

Django attaches the uploads in the FILE dictionary variable on the request object which you can access like so:

for uploaded_file in request.FILES['file']:
    # process the file

which could look something like:

# views.py
from django.http import HttpResponseRedirect

def upload_file(request):
    for uploaded_file in request.FILES.getlist('file'):
        handle_uploaded_file(uploaded_file)

    return HttpResponseRedirect(...)


def handle_uploaded_file(f):
    destination = open('/upload/path/%s' % f.name, 'wb+')

    for chunk in f.chunks():
destination.write(chunk) destination.close()

comments