1

I am using a DecimalField in my model as I need quite a few decimal places for some of the values. But for some of the values I don't need as many zeroes. The problem is in the same field I will have some values that are 0.76 and other values that are 0.0001548337258. Unfortunately FloatField is not accurate enough for my needs.

But when I use:

 class Part(models.Model):
      dec_number = models.DecimalField(max_digits=19, decimal_places=15)

I unfortunately get the same amount of empty 0's on my 0.76.

I would like the output when I call that field to be 0.76 in that location, and 0.00000037. Currently my output is:

 0.760000000000000
 0.000154833725800

I am trying to get my output to be:

 0.76
 0.0001548337258

Is there a way to remedy this?


Edit:

Normalize works to drop the decimals. But I am not sure where to implement that normalize method to the DecimalField in Django.

I tried to do it in the views.py file but got this error.

 AttributeError: 'DeferredAttribute' object has no attribute 'normalize'

Here is a minimal version of my views.py that I tried..

 def parts(request):
      part = get_object_or_404(Part, pk=part_pk)
      noZero = Part.dec_number
      noZero.normalize()
      return render(request, part.html, {'noZero':noZero})

I also tried:

 def parts(request):
      part = get_object_or_404(Part, pk=part_pk)
      noZero = Part.dec_number.normalize()
      return render(request, part.html, {'noZero':noZero})

I tried normalizing in models.py:

 class Part(models.Model):
     part_material_density = models.DecimalField(max_digits=19, decimal_places=15)
     noZeroDen = dec_number.normalize()

But that gives me the error:

 AttributeError: 'DecimalField' object has no attribute 'normalize'


What worked:

In my views.py I changed the Part.dec_number.normalize() to part.dec_number.normalize()

def parts(request):
      part = get_object_or_404(Part, pk=part_pk)
      noZero = part.dec_number.normalize()
      return render(request, part.html, {'noZero':noZero})

Something Else I Struggled with:

I couldn't figure out how to get each item in the model to normalize before outputting to my HTML page.

I ended up looping through each part within my views.py and normalizing the field itself, instead of changing it's name. Hopefully this will not cause issues in the future, but I don't believe it will, and everything seems to be working as intended.

Here is the loop I used.

def parts(request):
    parts = Part.Objects.all()

    for part in parts:
        part.dec_number = part.dec_number.normalize()

    return render(request, 'parts.html', {'parts':parts})

Hopefully this can help someone else!

Shyrtle
  • 647
  • 5
  • 20

1 Answers1

1

Use normalize()--(PyDocs) method

part_instance.dec_number.normalize()
JPG
  • 82,442
  • 19
  • 127
  • 206
  • At what level do I normalize it? I tried to do it in the views.py file, but I get an error: AttributeError: 'DeferredAttribute' object has no attribute 'normalize' – Shyrtle Feb 27 '20 at 15:51
  • 1
    at the object level. Can you add the error traceback and "minimal code of views.py" to the question? – JPG Feb 27 '20 at 15:54
  • Ok, I added the code. At the object level? Wouldn't that be in the views.py method? – Shyrtle Feb 27 '20 at 16:00
  • 1
    Change the line, **`noZero = part.dec_number.normalize()`** – JPG Feb 27 '20 at 16:02