Python returning Decimal(x.x) from objects. in SQL DB

I have a table that stores current values used to provide the data to render some charts.

One field in the model is a decimal, and when I get the values the list returned is of the format [Decimal:(x.x)…].

I’m guessing this is a python issue and not django; although I guess I could save the values in a CharField and convert to numerals…

I am trying to figure out what I am doing wrong, as all I want is the numerical values in the chart.

models.py:

class Final_Result(models.Model):
    user_id = models.IntegerField(null = True, blank=False)
    area = models.CharField(max_length=300)
    scores = models.DecimalField(max_digits=2, decimal_places=1)
    max_scores=models.IntegerField(blank = True)
    survey = models.IntegerField(blank = True)
    company = models.CharField(max_length=250, null = True)
    area_id = models.IntegerField(null = True, blank=False)
    overall_color = models.CharField(max_length=20, null = True)
    survey_name = models.CharField(max_length=40, null = True)
    length = models.IntegerField(blank = True)

    def __str__(self):
        return f"{self.user_id} company {self.company} survey {self.survey} area {self.area} score {self.scores} max {self.max_scores} "

Views.py that get the data:

def results_overall(request):
    #user_id=request.user.id
    user_id=1
    area_name = list(Final_Result.objects.filter(user_id = user_id).values_list("area", flat=True))
    area_scores = list(Final_Result.objects.filter(user_id = user_id).values_list("scores", flat=True))
    print('area: ', area_name)
    print('area score: ', area_scores)

    return render(request,'ISO22301/results_overall.html')

Print output:

area:  ['Risk Management Framework and Leadership', 'Organizational Context', 'Monitoring and Review', 'Communication and Consultation', 'Risk Response', 'Risk Assessment Process', 'Context of the Organization', 'Improvement', 'Mangement Review', 'Operations', 'Support', 'Policy and Planning', 'Leadership', 'Leadership and Governance', 'Long-Term Resilience Strategy', 'Supply Chain Resilience', 'Adaptation and Learning', 'Collaboration and Communication', 'Performance Monitoring and Improvement', 'Resource Management and Adaptability', 'Business Continuity and Crisis Management', 'Risk Management', 'Organizational Culture and Awareness']
area score:  [Decimal('0.0'), Decimal('0.0'), Decimal('0.0'), Decimal('0.0'), Decimal('0.0'), Decimal('0.0'), Decimal('1.0'), Decimal('1.0'), Decimal('1.0'), Decimal('1.0'), Decimal('2.0'), Decimal('2.0'), Decimal('2.0'), Decimal('1.0'), Decimal('3.0'), Decimal('3.0'), Decimal('2.0'), Decimal('3.0'), Decimal('4.0'), Decimal('5.0'), Decimal('4.0'), Decimal('3.0'), Decimal('2.0')]

If I get Max_scores it is formatted as expected:

MAx score:  [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]

You are not doing anything wrong, other than assuming that the print function is giving you the same representation of an object as the value(s) contained by that object.

When you render those values in the template, they will render correctly. There is no conversion that you need to do.

Always remember that the “print” function on an object prints a representation of that object as defined by that object - which may have no relationship to any of the values stored in that object.

Side note: Unless you have a specific need to do this, you generally do not want to use the values clause in queries for objects to be rendered in a view. You lose a lot of flexibility and don’t really gain anything.

1 Like

So would it be better to do something like:

survey_results = Final_Result.objects.filter(user_id = user_id)
survey_results_area = list(survey_results.values('area'))
survey_results_scores = list(survey_results.values('scores'))

and get each set of values as subsets?

What is the disadvantage of values.list vs. using subsets?

I do appreciate your help and patience as I learn.

No, these aren’t needed at all.

Pass the objects directly to the template in the context. Render the members of the querysets in the templates.

You might want to review the work you did in the Django tutorial, particularly part 3

(You haven’t shown your template here, so we can’t really get very specific as to what your better options may be.)

1 Like

That makes sense since I can as easily access each value from the master set.

I haven’t actually written the template yet, just breaking down each step into subtasks so as to get one working before I go on to the next step.

What I was doing was getting the values so I can convert them to JSON for use in a script that generates the graphs. I found it clearer and easier for me to create QuerySets for each set of values and convert them. I then can use the format score[n] to get the correct value for each data point, for example.

I’ll have to explore how to break down the master set into subsets in JSON format to use it the script.

That may change the recommendation. This might fit into one of those cases where it’s helpful to pre-create these as data.

You will also want to read about the json_script filter. When passing data through a template to JavaScript, you do not want to try to render the data object directly.

1 Like

Yup, that’s what I’ve been using. It’s probably easier to send each as a separate QuerySet than trying to filter the JSON, as you point out.

Thanks for your help.