Showing posts with label django. Show all posts
Showing posts with label django. Show all posts

Sunday, February 21, 2010

Django db lists

Keep forgetting this one.

To get data from the database and convert into a list in Django

Entry.objects.values_list('id', flat=True).order_by('id')

Wednesday, February 17, 2010

Server Hosting

Few problems with hosting. Shared hosting is proving to be a problem. For starters, no SSH access means we pretty much can't run/install anything other than CMS systems which is pretty rubbish.

We considered going to the cloud, as we're not exactly expecting mammoth numbers of hits. The cloud, the latest buzz, is the amazon EC2 affair. The future of web development. Even comes with a calculator.

http://calculator.s3.amazonaws.com/calc5.html

Trouble with the cloud is, like most things, strip away the silky velure and you realise that this is a whole pile of shite. For a prototype site you're looking at 500+ per year for a VPS (virtual private server). So great for those who want to upscale large sites and use resource "bursts". Crap for us.

So gonna use plain old VPS on our irish host at 18 euro per month. Functional, cheap, not so pretty but should work, like a wank sock.

Thursday, February 11, 2010

MySQLdb import setuptools registry key blah

For my nth time reinstalling python and setting up MySQL on a windows machine, i'm back to the same list of errors

ImportError: No module named setuptools

I go find setup tools and install this. Next:

File "setup.py", line 15, in
metadata, options = get_config()
File "C:\Software\MySQL-python-1.2.3c1\setup_windows.py", line 7, in get_confi
g
serverKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, options['registry_ke
y'])
WindowsError: [Error 2] The system cannot find the file specified

Gotta love these errors. Completely unintuitive for a relatively tiny and essential package.

The solution???

Give up trying to use the latest release, maybe it's optimised for linux anoraks and use:

http://www.technicalbard.com/files/MySQL-python-1.2.2.win32-py2.6.exe

This installer will sort out everything

Wednesday, February 10, 2010

Filtering list in Django

Right so .filter is the way to get data from a query set. How do you go about filtering a set based on on a different set?

For SQL heads this is usually done by WHERE ID IN () or WHERE ID CONTAINS()

What you need to do in Django is convert the () part into a list and filter as normal

random_questions = All_Questions.objects.filter(pk__in=list(ordered_questions.values_list('id', flat=True))).order_by('?')

Flat = true implies the results are returned efficiently [1,2,3] as opposed to [(1,),(2,),(3,)]

Tuesday, February 9, 2010

Extending Django user model

Right so using the nifty Django user model is easy but how do i create a foreign key reference?

First use:
from django.contrib.auth.models import User

This gives access to the user model. Then just use the user as you would a normal foreign key reference
user_fk = models.ForeignKey(User)