ORM - Join - Related column shows blank

I have the following models defined…

class Modules(models.Model):
    module_code = models.CharField(max_length=5)
    module_name = models.CharField(max_length=100)
    datetimestamp = models.DateTimeField(auto_now=True)

    class Meta:
        # Defines the name to appear in the Admin section
        verbose_name_plural = "Modules"


class ChangeLogEntries(models.Model):
    change_date = models.DateField()
    change_note = models.TextField()
    form_name = models.CharField(max_length=100)
    datetimestamp = models.DateTimeField(auto_now=True)
    module_code = models.CharField(max_length=5)
    binary_only = models.BooleanField(default=False)
    installed = models.BooleanField(default=False)
    dateinstalled = models.DateField(blank=True, null=True)
    module_category = models.ForeignKey(Modules, default=1, verbose_name="Module",
                                        on_delete=models.SET_DEFAULT, null=True)

Following is the code from my view:

def home(request):
    change_list = ChangeLogEntries.objects.all()[:4].select_related('module_category')
    the_query = change_list.query
    context = {'change_list': change_list, 'query': the_query}
    return render(request, 'main/home.html', context)

In the template, I am trying to display the module_name column. When I view the_query I am seeing this listed as one of the columns returned…

SELECT changeLOGX_changelogentries.id, changeLOGX_changelogentries.change_date, changeLOGX_changelogentries.change_note, changeLOGX_changelogentries.form_name, changeLOGX_changelogentries.datetimestamp, changeLOGX_changelogentries.module_code, changeLOGX_changelogentries.binary_only, changeLOGX_changelogentries.installed, changeLOGX_changelogentries.dateinstalled, changeLOGX_changelogentries.module_category_id, changeLOGX_modules.id, changeLOGX_modules.module_code, changeLOGX_modules.module_name, changeLOGX_modules.datetimestamp FROM changeLOGX_changelogentries LEFT OUTER JOIN changeLOGX_modules ON (changeLOGX_changelogentries.module_category_id = changeLOGX_modules.id) LIMIT 4

Here is the template where I am trying to show the module_name

                <div class="row">
                     {% for x in change_list %}
                    <div class="col-lg-4 col-md-6 col-sm-12">
                        <div class="card mt-2">
                          <div class="card-header">
                              {{ x.module_name }}!!
                          </div>
                          <div class="card-body">
                            <h5 class="card-title">{{ x.change_date }}</h5>
                            <p class="card-text" style="text-align: left">{{ x.change_note}}</p>
                            <p class="card-text" style="text-align: left">
                                 {% if x.installed %}
                                    <strong>Installed</strong>
                                 {% else %}
                                     <p style="font-size: smaller; font-style: italic">
                                        Note: not installed yet
                                     </p>
                                {% endif %}
                            </p>
                          </div>
                        </div>
                    </div>
                    {% endfor %}
                </div>

However, x.module_name is not appearing. If I change it to x.module_category_id it does display.

What am I missing here?

Thanks!
BH

In the context for this view, x is an instance of ChangeLogEntries. It has an attribute named module_category, a ForeignKey referencing the Modules model.

The module_name you’re wanting to render is an attribute of Modules.

Using x, you first need to retrieve the related instance of Modules - that would be x.module_category. You can then access that attribute as x.module_category.module_name.

That was it!!

                          <div class="card-header">
                              {{ x.module_category.module_name }}
                          </div>

Thanks again for the help Ken!