Re: simple multiplication in models
Fabio, The only reasons I can think of for this kind of denormalization of the database are performance (as you mentioned, unsignificant benefit here) or using the resulting value in a filter condition. The latter can be better worked around by mixing raw SQL with Django's ORM expressions (with the extra= keyword). --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: simple multiplication in models
Chris Amico wrote: [...] > Awesome. That worked. Thanks all. > > > 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 --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: simple multiplication in models
Awesome. That worked. Thanks all. On Aug 25, 4:14 pm, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote: > On Mon, 2008-08-25 at 15:58 -0700, Chris Amico wrote: > > 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'") > > 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 --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: simple multiplication in models
On Mon, 2008-08-25 at 15:58 -0700, Chris Amico wrote: > 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'") > 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 --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: simple multiplication in models
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 -~--~~~~--~~--~--~---
Re: simple multiplication in models
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 -~--~~~~--~~--~--~---
Re: simple multiplication in models
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 -~--~~~~--~~--~--~---