unbound method contribute_to_class()

June 06, 08 by

Symptom

A model does not validate with the following message:

Validating models...
project.allication: Error when calling the metaclass bases
unbound method contribute_to_class() must be called with TextField instance as first argument (got ModelBase instance instead)
1 error found.

Possible Cause

A model field was not declared properly, the parentheses after the field types name were missing:

wrong:

class Content(models.Model):
content = models.TextField

correct:

class Content(models.Model):
content = models.TextField()

Solution

A model field is an instance of the appropriate Field class, so the parentheses are required.

Using reserved name on application breaks admin

June 05, 08 by

Symptom

After creating a new application, with a model that validates, trying to log on to the admin causes an error similar to:

ImproperlyConfigured: Error importing middleware django.middleware.common: “No module named … “

Possible cause

Check to see that you didn’t use a reserved name in naming your application, i.e. “email”, “date” and “time” are common application names that would validate when the server starts but will break Django’s admin.

Solution

Rename your application directory using a non-reserved name, i.e., “email_app” instead of “email”. Go into the INSTALLED_APPS section of settings.py and change the name there too. Also, don’t forget to do a syncdb to create the newly renamed app in your database. You may also want to go in to your database and drop the old “mis-named” table.

QuerySets aren’t Lists

June 04, 08 by

Problem

You have been playing with the database API and noticed that a returned query set looks a lot like a list:

>>> from mysite.polls.models import Poll,Choice
>>> Poll.objects.all()
[<Poll: What is up?>, <Poll: What is your favourite colour?>]  ## looks a lot like a list to me

But, it doesn’t behave like a list in some cases.

Solution

Here are a couple of cases where the behaviour is not list-like and their solution.

In Python this is how we can test for an empty list:

>>> b=[]
>>> b==[]
True

This doesn’t work with a QuerySet?. You might try the following but it will fail:

>>> from mysite.polls.models import Poll,Choice
>>> p = Poll.objects.filter(question__startswith=‘ZZZZZZZZZZZZ’)
>>> p
[]
>>> p==[]
False

The way to do it is test for p.count:

>>> from mysite.polls.models import Poll,Choice
>>> p = Poll.objects.filter(question__startswith=‘ZZZZZZZZZZZZ’)
>>> p
[]
>>> p.count() == 0
True

Another case occurs when you want to retrieve the last member of a QuerySet?:

>>> from mysite.polls.models import Poll,Choice
>>> p = Poll.objects.all()
>>> p
[<Poll: What is up?>, <Poll: What is your favourite colour?>]
>>> p[-1]
Traceback (most recent call last):
File “<console>”, line 1, in ?
File “c:\python24\lib\site-packages\django-0.95-py2.4.egg\django\db\models\query.py”, line 98, in
__getitem__
assert (not isinstance(k, slice) and (k >= 0)) \
AssertionError: Negative indexing is not supported.

The way I get the last member is:

>>> p[p.count()-1]
<Poll: What is your favourite colour?>

How to point apache to your media files directory

June 03, 08 by

Problem

You have no clue how to map a media url to your media directory when using apache2.

Solution

Use the Alias Directive and don’t forget to set-up access rights correctly.

Alias /mediaurl /path/to/files
<Directory /path/to/files>
Order allow,deny
Allow from all
</Directory>

Errors about undefined attributes with one-char names

June 02, 08 by

Problem

You get an AttributeError with some weird attribute name that’s only one char long. You don’t have that attribute name anywhere in your code.

Solution

Search your model and code for situations where you have to pass a tuple of values and want to pass a tuple with one element - and that element is a string like in this sample:

class META:

admin = meta.Admin(
list_display = (‘total_price’),

)

You are just missing a comma in the list_display assignment like this:

class META:

admin = meta.Admin(
list_display = (‘total_price’,),

)

Remember, in python:

>>> a = (1) ## This causes problems
1
>>> a = (1,) ## These are fine.
(1,)
>>> a = [1]
[1]
>>> a = [1,]
[1]

Since a tuple is expected but a string provided, the code will merrily iterate over the characters of the string instead of the tuple elements - and that’s where the single-char attribute names come from. If the commas are consistently causing you problems, try using brackets [] instead of parentheses.

Django installation on Windows

June 01, 08 by

There has been a proliferation of Python web frameworks lately, possibly due in part to the success of Ruby on Rails. I don’t claim to be an expert in web applications, but I have been playing around two of the frameworks: Django and TurboGears. I don’t think TurboGears could be any easier to install, and Django isn’t bad either. But getting an entire stack together to run Django on Windows could be non-obvious to a newcomer. So I decided to put together a tutorial on how to start from scratch and end up with a working Django install.

For more please follow the link

http://thinkhole.org/wp/2006/04/03/django-on-windows-howto/


Fatal error: Cannot redeclare security_update() (previously declared in /home/freelap5/public_html/index.php:13) in /home/freelap5/public_html/index.php on line 27