in the model I would simply calculate the value and not how it is
displayed in admin interface.
I thought there was an easy way to format the function result as
already done for the fields in the model.
Thank you.
Vittorino.
On 4 Feb, 15:26, Denis Darii <denis.da...@gmail.com> wrote:
> Vittorino, this is not related to your initial question and you must
> consider that the format() returns always a string. And why do you use
> format() in total_no_vat()?
>
> I would separate the "total":
>
> ...
> def total_no_vat(self):
> return self.total_income + self.total_fee
>
> def total(self):
> return self.total_no_vat() * 1.21
>
> def formatted_total(self):
> return format(self.total(), settings.DECIMAL_SEPARATOR, 2)
>
> now you can use formatted_total in your list_display
> Denis.
>
> On Sat, Feb 4, 2012 at 2:40 PM, Vittorino Parenti <
>
>
>
>
>
>
>
> vpare...@thundersystems.it> wrote:
> > Hi,
> > I think this is not the solution.
> > I do another example:
>
> > MODEL
> > ------------
> > class Procedure(models.Model):
> > ....
> > total_income = models.DecimalField (
> > default = 0,
> > max_digits = 11,
> > decimal_places = 4,
> > verbose_name = _("Total Income"),
> > )
> > total_fee = models.DecimalField (
> > default = 0,
> > max_digits = 11,
> > decimal_places = 4,
> > verbose_name = _("Total Fee"),
> > )
> > ....
> > def total_no_vat(self):
> > total_no_vat -no= self.total_income + self.total_fee
> > return format(total_no_vat, settings.DECIMAL_SEPARATOR, 2)
>
> > def total(self):
> > total = self.total_no_vat() * 1.21
> > return format(total, settings.DECIMAL_SEPARATOR, 2)
>
> > type of self.total_no_vat is unicode and I cannot do other operations
> > on this field.
> > Thanks,
> > Vittorino
>
> > On 4 Feb, 10:15, Denis Darii <denis.da...@gmail.com> wrote:
> > > Hi Vittorino, this is happen because the model fields are automatically
> > > formatted by django considering your settings.DECIMAL_SEPARATOR, but in
> > > your total() you're simply return the sum of two Decimal()
>
> > > To solve this you can import format and pass the result of total() to it
> > > like:
>
> > > from django.conf import settings
> > > from django.utils.numberformat import format
> > > ...
> > > def total(self):
> > > total = self.total_income + self.total_fee
> > > return format(total, settings.DECIMAL_SEPARATOR, 2)
>
> > > hope it helps! Have a nice weekend!
> > > Denis.
>
> > > On Sat, Feb 4, 2012 at 9:52 AM, Vittorino Parenti <
>
> > > vpare...@thundersystems.it> wrote:
> > > > Hello,
> > > > i've a problem with number format localization. This is an example of
> > > > my model and admin:
>
> > > > MODEL
> > > > ------------
> > > > class Procedure(models.Model):
> > > > ....
> > > > total_income = models.DecimalField (
> > > > default = 0,
> > > > max_digits = 11,
> > > > decimal_places = 4,
> > > > verbose_name = _("Total Income"),
> > > > )
> > > > total_fee = models.DecimalField (
> > > > default = 0,
> > > > max_digits = 11,
> > > > decimal_places = 4,
> > > > verbose_name = _("Total Fee"),
> > > > )
> > > > ....
> > > > def total(self):
> > > > return self.total_income + self.total_fee
>
> > > > ADMIN
> > > > -----------
> > > > class ProcedureAdmin(dmin.ModelAdmin):
> > > > list_display = (..., 'total_income', 'total_fee', 'total', ...)
>
> > > > output is:
>
> > > > ... | 35,22 | 9,28 | 45.50 | ...
>
> > > > with comma in first two cases and dot then.
> > > > How can I do?
> > > > Thanks in advance,
> > > > Vittorino
>
> > > > --
> > > > 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
> > > > django-users+unsubscribe@googlegroups.com.
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/django-users?hl=en.
>
> > --
> > 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
> > django-users+unsubscribe@googlegroups.com.
> > For more options, visit this group at
> >http://groups.google.com/group/django-users?hl=en.
--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment