Your earlier model had a bug -- two things called total_cost. The second
thing with that name (the method) completely hid the first thing. It
wasn't just something that gave Karen a headache; it was a real bug. So
when you ran "syncdb", there was no field called total_cost because the
"total_cost" attribute was a method. Thus, you have added a new field to
the model now that you have removed the method.
You'll either have to update the database table manually, or drop and
recreate that model's table (look at the sqlreset command for
django-admin.py).
Regards,
Malcolm
> > > Class Item(models.Model):
> > > name = models.CharField(max_length=100)
> > > price = models.DecimalField(max_digits=9, decimal_places=2)
> > > quantity = models.IntegerField(default=1)
> > > total_cost = models.DecimalField(max_digits=12,
> > > decimal_places=2,
> > > blank=True,
> > > null=True,
> > > editable=False)
> >
> > > def calc_total(self):
> > > amount = (self.price * self.quantity)
> > > return amount
> >
> > > def save(self):
> > > self.total_cost = self.calc_total()
> > > super(Item, self).save()
I am very happy to hear that everything's working now.
I am a bit confused though: why not just dropping the total_cost field
from the table? You can retrieve that value on the fly in your views,
no need to have it "hardcoded" in your db. (It's a simple
multiplication, I guess you won't have performance issue for this
change.)
I'd appreciate if anyone can shed some light on this and point out any
drawbacks I can't see.
All the best, Fabio.
--
Fabio Natali