On Mon, Aug 25, 2008 at 5:22 PM, Chris Amico <[EMAIL PROTECTED]> wrote:
> > This seems like it should be simple, but I'm getting errors. > > I have a model tracking individual purchases. User inputs an item cost > and quantity; I want the total cost calculated. Here are the relevant > parts: > > 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 total_cost(self): > total = (self.price * self.quantity) > return total_cost(total) > > def save(self): > self.total_cost = self.total_cost() > super(Item, self).save() > > The error I get is: > > NameError at /admin/spending/item/add/ > global name 'total_cost' is not defined > > Not sure what that means. Thoughts? Am I missing an exceedingly > obvious way to do this? > You're making my brain explode trying to use the single name 'total_cost' for both a model field and a method. I'd name the method that computes the values of the 'total_cost' field something else, like 'compute_total_cost'. Then, within it, be very careful to preface all references to model field values with 'self.'. The error message you are getting usually happens when you forget a 'self.' in front of a reference to a model field. Karen --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---