django.db.models.DecimalField - python examples

Here are the examples of the python api django.db.models.DecimalField taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

37 Examples 7

3 View Complete Implementation : converter.py
Copyright MIT License
Author : eamigo86
@convert_django_field.register(models.DecimalField)
@convert_django_field.register(models.FloatField)
@convert_django_field.register(models.DurationField)
def convert_field_to_float(field, registry=None, input_flag=None, nested_field=False):
    return Float(
        description=field.help_text or field.verbose_name,
        required=is_required(field) and input_flag == "create",
    )

3 View Complete Implementation : models.py
Copyright MIT License
Author : gnosis
    def with_balance(self):
        """
        :return: Queryset with the Safes and a `balance` attribute
        """
        return self.annotate(
            balance=Subquery(
                InternalTx.objects.balance_for_all_safes(
                ).filter(
                    to=OuterRef('address')
                ).values('balance').distinct(),
                models.DecimalField()))

3 View Complete Implementation : test_case.py
Copyright MIT License
Author : labd
    def astertModelDecimalField(self, field, max_digits, decimal_places, null=False, blank=False):
        self.astertEqual(field.__clast__, models.DecimalField)
        self.astertEqual(field.max_digits, max_digits)
        self.astertEqual(field.decimal_places, decimal_places)
        self.astertEqual(field.null, null)
        self.astertEqual(field.blank, blank)

3 View Complete Implementation : test.py
Copyright MIT License
Author : martsberger
    def test_pivot_aggregate(self):
        shirt_sales = ShirtSales.objects.all()

        data = ExpressionWrapper(F('units') * F('price'), output_field=DecimalField())
        pt = pivot(ShirtSales, 'store__region__name', 'shipped', data, Avg, default=0)

        for row in pt:
            region_name = row['store__region__name']
            for dt in (key for key in row.keys() if key != 'store__region__name'):
                spends = [ss.units * ss.price for ss in shirt_sales if force_text(ss.shipped) == force_text(dt) and ss.store.region.name == region_name]
                avg = sum(spends) / len(spends) if spends else 0
                self.astertAlmostEqual(row[dt], float(avg), places=4)

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_combine_different_types(self):
        msg = 'Expression contains mixed types. You must set output_field.'
        qs = Book.objects.annotate(sums=Sum('rating') + Sum('pages') + Sum('price'))
        with self.astertRaisesMessage(FieldError, msg):
            qs.first()
        with self.astertRaisesMessage(FieldError, msg):
            qs.first()

        b1 = Book.objects.annotate(sums=Sum(F('rating') + F('pages') + F('price'),
                                   output_field=IntegerField())).get(pk=self.b4.pk)
        self.astertEqual(b1.sums, 383)

        b2 = Book.objects.annotate(sums=Sum(F('rating') + F('pages') + F('price'),
                                   output_field=FloatField())).get(pk=self.b4.pk)
        self.astertEqual(b2.sums, 383.69)

        b3 = Book.objects.annotate(sums=Sum(F('rating') + F('pages') + F('price'),
                                   output_field=DecimalField())).get(pk=self.b4.pk)
        self.astertEqual(b3.sums, Approximate(Decimal("383.69"), places=2))

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_multi_arg_aggregate(self):
        clast MyMax(Max):
            output_field = DecimalField()

            def as_sql(self, compiler, connection):
                copy = self.copy()
                copy.set_source_expressions(copy.get_source_expressions()[0:1])
                return super(MyMax, copy).as_sql(compiler, connection)

        with self.astertRaisesMessage(TypeError, 'Complex aggregates require an alias'):
            Book.objects.aggregate(MyMax('pages', 'price'))

        with self.astertRaisesMessage(TypeError, 'Complex annotations require an alias'):
            Book.objects.annotate(MyMax('pages', 'price'))

        Book.objects.aggregate(max_field=MyMax('pages', 'price'))

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_decimal_field(self):
        field = models.DecimalField(max_digits=5, decimal_places=2)
        name, path, args, kwargs = field.deconstruct()
        self.astertEqual(path, "django.db.models.DecimalField")
        self.astertEqual(args, [])
        self.astertEqual(kwargs, {"max_digits": 5, "decimal_places": 2})

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_decimal_field_0_decimal_places(self):
        """
        A DecimalField with decimal_places=0 should work (#22272).
        """
        field = models.DecimalField(max_digits=5, decimal_places=0)
        name, path, args, kwargs = field.deconstruct()
        self.astertEqual(path, "django.db.models.DecimalField")
        self.astertEqual(args, [])
        self.astertEqual(kwargs, {"max_digits": 5, "decimal_places": 0})

3 View Complete Implementation : test_decimalfield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_to_python(self):
        f = models.DecimalField(max_digits=4, decimal_places=2)
        self.astertEqual(f.to_python(3), Decimal('3'))
        self.astertEqual(f.to_python('3.14'), Decimal('3.14'))
        # to_python() converts floats and honors max_digits.
        self.astertEqual(f.to_python(3.1415926535897), Decimal('3.142'))
        self.astertEqual(f.to_python(2.4), Decimal('2.400'))
        # Uses default rounding of ROUND_HALF_EVEN.
        self.astertEqual(f.to_python(2.0625), Decimal('2.062'))
        self.astertEqual(f.to_python(2.1875), Decimal('2.188'))
        msg = "'abc' value must be a decimal number."
        with self.astertRaisesMessage(ValidationError, msg):
            f.to_python('abc')

3 View Complete Implementation : test_cast.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    @skipUnlessDBFeature('supports_cast_with_precision')
    def test_cast_to_decimal_field(self):
        FloatModel.objects.create(f1=-1.934, f2=3.467)
        float_obj = FloatModel.objects.annotate(
            cast_f1_decimal=Cast('f1', models.DecimalField(max_digits=8, decimal_places=2)),
            cast_f2_decimal=Cast('f2', models.DecimalField(max_digits=8, decimal_places=1)),
        ).get()
        self.astertEqual(float_obj.cast_f1_decimal, decimal.Decimal('-1.93'))
        self.astertEqual(float_obj.cast_f2_decimal, decimal.Decimal('3.5'))
        author_obj = Author.objects.annotate(
            cast_alias_decimal=Cast('alias', models.DecimalField(max_digits=8, decimal_places=2)),
        ).get()
        self.astertEqual(author_obj.cast_alias_decimal, decimal.Decimal('1'))