Access to FK table fields in template not working

Hi, I hope someone can find the wrong part. I’m not able to get access to the model fields of the pk. Normally this should be a simple thing, but for any reason it doesn’t work here. Any help appreciated.

class PlanningWeek(models.Model):

    monday = models.ForeignKey('PlanningDay', on_delete=models.CASCADE, related_name='weeks_monday', blank=True, default='1')
    tuesday = models.ForeignKey('PlanningDay', on_delete=models.CASCADE, related_name='weeks_tuesday', blank=True, default='1')
    wednesday = models.ForeignKey('PlanningDay', on_delete=models.CASCADE, related_name='weeks_wednesday', blank=True, default='1')
    thursday = models.ForeignKey('PlanningDay', on_delete=models.CASCADE, related_name='weeks_thursday', blank=True, default='1')
    friday = models.ForeignKey('PlanningDay', on_delete=models.CASCADE, related_name='weeks_friday', blank=True, default='1')
    week_of_year = models.IntegerField("Week Number")
    year_of_obj = models.IntegerField("Year", choices = YEAR_CHOICES, default =
    def __str__(self):
        return f'Week: {self.week_of_year}'

class PlanningDay(models.Model):
    meat_meal = models.ForeignKey('Meal', on_delete=models.CASCADE, related_name='meat_meal_planning_day_set')
    veg_meal = models.ForeignKey('Meal', on_delete=models.CASCADE, related_name='veg_meal_planning_day_set')
    date_of_day = models.DateField("Date")
    calendar_week = models.IntegerField('Calendar week')
    def __str__(self):
        if > 1:
            return f'Day: {self.date_of_day} / KW: {self.calendar_week}'
            return f'n/a'

class UpdateWeek(ListView):
    model = PlanningWeek
    template_name = 'mealOrder/show_week.html'
#for testing its limited to one entry, because this one has all values in db
    def get_queryset(self, *args, **kwargs): 
        qs = super().get_queryset(*args, **kwargs)
        qs = PlanningWeek.objects.filter(week_of_year = '29').values()
        return qs


{% for week in planningweek_list %}
<div class="container">
    <div class="row">
        <div class="col">
            <h4>{{ week.monday.date_of_day }}</h4>

As you can see pk-fields are filled, but the template rendering results in an empty
<h4>-tag. If I change to {{ week.monday_id } the correct id will be displayed. What am I doing wrong?

Welcome @svepo !

The issue is here:

Normally, you want to pass the objects in the context to the rendering engine. However, by using the values clause, you’re throwing away all the object data and only returning values.

Unless you specifically need to only pass values to the context, you almost always want to return objects.

1 Like

One side note is that using foreign object in template can be expensive if you don’t use select_related or prefetch_related.

Imagine if you have 10 objects, each time you call week.monday, you execute a query select * from monday_table where id = week.monday_id. So in total, you execute 10 queries in your template/view. Which is expensive.

Thank you, that’s it! I didn’t even looked at that part of the code …
@hoavq-glinteco : Thank you for that hint. In my template this won’t matter, because there will be only one call of week.monday. But I’ll keep that in mind!

Thank you both, you saved my day :slight_smile:

1 Like