简体   繁体   中英

Django Object Value Pull Resulting in Tuple?

New to Django and trying to understand a mysql query that I currently have working somewhat. It is pulling my database values but it is print this off as an array or tuple? I'm not sure exactly which one, I am assuming there is some .value like DFs but using object.value I thought would have done this? Seems so easy but after a few hours I need a nudge please.

view.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import PreviousLossesMlbV2


def show_view(request):
    games = PreviousLossesMlbV2.objects.values('actual_over_under_result_field')
    results = PreviousLossesMlbV2.objects.values('actual_over_under_result_field')
    return render(request,"show.html", {'games': games, 'results': results})

show.html

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Django CRUD Operations</title>
  <meta charset="utf-8">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<table class="table table-striped">
    <thead>
    </thead>
    <tbody>
      <tr>
          {% for game in games %}
        <p>{{ game }}</p>
{% endfor %}
          {% for result in results %}
        <p>{{ result }}</p>
      </tr>
{% endfor %}
    </tbody>
</table>
</div>
</body>
</html>

This works, but the problem is, here is the values displayed on the webpage.

"{'actual_over_under_result_field': 'OVER'}

{'actual_over_under_result_field': None}

{'actual_over_under_result_field': 'OVER'}

{'actual_over_under_result_field': 'UNDER'}

{'actual_over_under_result_field': 'OVER'}

{'actual_over_under_result_field': 'OVER'}

{'actual_over_under_result_field': 'OVER'}

{'actual_over_under_result_field': 'OVER'}

{'actual_over_under_result_field': 'UNDER'}

{'actual_over_under_result_field': 'UNDER'}

{'actual_over_under_result_field': 'OVER'}

{'actual_over_under_result_field': 'UNDER'}

{'actual_over_under_result_field': 'UNDER'}

{'actual_over_under_result_field': 'OVER'}

{'actual_over_under_result_field': None}

{'actual_over_under_result_field': 'OVER'}

{'actual_over_under_result_field': 'UNDER'}

{'actual_over_under_result_field': 'OVER'}

{'actual_over_under_result_field': 'OVER'}

{'actual_over_under_result_field': 'OVER'}

{'actual_over_under_result_field': 'OVER'}

{'actual_over_under_result_field': 'UNDER'}

{'actual_over_under_result_field': 'UNDER'}

{'actual_over_under_result_field': 'OVER'}

{'actual_over_under_result_field': 'UNDER'}

{'actual_over_under_result_field': 'UNDER'}"

I'm only looking for the "UNDER" or "OVER" value to display.

您需要带有flat=True values_list

results = PreviousLossesMlbV2.objects.values_list('actual_over_under_result_field', flat=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM