Changed the variable names to make it a little more readable. Method
now returns "amount" (formerly "total"):

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()


Now I get a different error when I try to save an Item:

OperationalError at /admin/spending/item/add/
(1054, "Unknown column 'total_cost' in 'field list'")



On Aug 25, 3:45 pm, nicksergeant <[EMAIL PROTECTED]> wrote:
> Should 'total_cost' simply:
>
> return total
>
> instead of:
>
> return total_cost(total)
>
> On Aug 25, 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 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to