Search code examples
djangodjango-modelsdjango-database

Django Database Queryset No Field Information


When I run a queryset through my views and pass it to the template all I get is the following:

<QuerySet [<ChangeLog: ChangeLog object (1)>, <ChangeLog: ChangeLog object (2)>, <ChangeLog: ChangeLog object (3)>, <ChangeLog: ChangeLog object (4)>, <ChangeLog: ChangeLog object (5)>, <ChangeLog: ChangeLog object (6)>, <ChangeLog: ChangeLog object (7)>, <ChangeLog: ChangeLog object (8)>, <ChangeLog: ChangeLog object (9)>, <ChangeLog: ChangeLog object (10)>, <ChangeLog: ChangeLog object (11)>]

How do I show the fields dictionary through my query?

views method:

def changeres(request):
    if request.user.is_authenticated:
            name = request.session.get('name')
            data = ChangeLog.objects.all()
            return render(request, 'changeres.html', {'data':  data})

Template:

{% extends "universal/header.html" %}
{% block content %}
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-4">
<div class="forms-content">
    <div class="tab-content" id="pills-tabContent">
            <p></p>
            <img class="mb-4" src="/static/FWIcon.png" alt="" width="100" height="100">
            <p></p>
            <div class="tab-pane fade show active" id="pills-home" role="tabpanel" aria-labelledby="pills-home-tab">
                            <form action="/change/action/" method="POST">
                            <p class="formtitle">Change Lookup</p>
                            <p></p>
                                    {% csrf_token %}
                                    <div class="form-group">
                                    <label class="fieldtitle"> Search Results </label>
                                    <table class="table">
                                    {{ data }}
                                    <thead>
                                    <th scope="col">a</th>
                                    <th scope="col">b</th>
                                    <th scope="col">c</th>
                                    <th scope="col">d</th>
                                    </thead>
                                    </table>
                                    </div>
                    <a href="/change/" class="btn btn-primary" >Submit</a>
            </form>
            </div>
    </div>
</div>
</main>
{% endblock %}

The {{ data }} is just there as a proof of concept till I can get it working properly


Solution

    1. You cannot display a queryset directly, you need to iterate thru it in your template.
    2. You can access the fields of your object in your template using object.myfieldname.

    Exemple:

    {% for item in data %}
       <p>item.one_of_my_fields</p>
    {% endfor %}
    

    You can also define a default string method for your class, as you can see django returns ChangeLog object (1) by default.

    Exemple:

    class Genre(models.Model):
    
        name = models.CharField(max_length=30, unique=True)
    
        def __str__(self):
            return self.name
    

    Now I could call my object directly in my template and it would return the name of the genre.

    {% for item in data %}
        <p>item</p>
    {% endfor %}