Blog Archive for May 14, 2010

Django DoesNotExist Accessing Attribute

May 14, 2010

I have a django model generated from a database. I was getting a really strange problem on one of the fields. This line of code:

self.linked_xml_class = pAttribute.linked_xml_class

failed with a DoesNotExist exception. Yet, if I dir(pAttribute) it does have that attribute on.

The problem occurred …

Modifying HTML with Django ModelForm

May 14, 2010

Sometimes you need to modify the HTML produced for a Django Model Form, for example to specify the number of columns to show in a TextArea.

Starting from a simple form:

from django import forms

class XmlClassForm(forms.ModelForm):
    class Meta:
        model = XmlClass
        fields = …

Django Primary Keys

May 14, 2010

If you have a Django model that has been generated from an existing database, you'll need to specify primary keys manually, by adding primary_key=True to the correct model field

id = models.IntegerField(primary_key=True, db_column='serial')

However, if the database auto populates the …