03/24: Beginner Django error
Posted by: bjb
I made a new django app. First I made a few models, then some unit tests for the models. I could not save newly created model instances in the database, the error was something about there not being an attribute id.
It turned out that although I inherited the models.Model class into each of my models, I was overriding the __init__ function. I should have called super from the derived class’s __init__ method … This is what the model class should have looked like:
from django.db import models class MyModel (models.Model): name = CharField (max_length = 64) description = CharField (max_length = 512) def __unicode__ (self): return self.name
and if i was going to declare an __init__ method, it should have looked like this:
def __init__(self, *args, **kwargs): super(MyClass, self).__init__(self, *args, **kwargs) # my own customization code goes here