# no able to link/load data on html from another table using the foreign key

**URL:** <https://forum.djangoproject.com/t/no-able-to-link-load-data-on-html-from-another-table-using-the-foreign-key/36762>\
**Category:** Using the ORM\
**Created:** [November 26, 2024, 1:51pm UTC](https://forum.djangoproject.com/t/no-able-to-link-load-data-on-html-from-another-table-using-the-foreign-key/36762 "2024-11-26T13:51:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lsaavedra21](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/lsaavedra21/32/24443_2.png) [@lsaavedra21](https://forum.djangoproject.com/u/lsaavedra21)\
**Post date:** [November 26, 2024, 1:51pm UTC](https://forum.djangoproject.com/t/no-able-to-link-load-data-on-html-from-another-table-using-the-foreign-key/36762/1 "2024-11-26T13:51:13Z")

</div>

Hello team.  
me again asking for a little help.  
I created 2 tables in model.py as shown below.

```auto
class c_training(SoftDelete):
    training_id = models.AutoField(primary_key=True)
    training_name = models.CharField(max_length=50, null=True)
    training_desc = models.CharField(max_length=150, null=True)
    
    created_dt = models.DateField(auto_now_add=True, null=True)
    last_modified_dt = models.DateField(auto_now=True, null=True)

    created_by = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, related_name='t_created_by') # add)
    updated_by = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, related_name='t_updated_by') # add) , editable=False

    def __str__ (self):
        return f"{self.training_name}"

```

```auto
class c_training_programed(SoftDelete):
    training_prog_id = models.AutoField(primary_key=True)
    training = models.ForeignKey(c_training, on_delete=models.CASCADE, related_name='c_training')
    trainer = models.ForeignKey(Member, on_delete=models.CASCADE, related_name='Member')
    places = models.IntegerField(null=True)
    places_available = models.IntegerField(null=True)
    inicial_dt = models.DateField(null=True)
    final_dt = models.DateField(null=True)
    total_hours = models.IntegerField(null=True)

    created_dt = models.DateField(auto_now_add=True, null=True)
    last_modified_dt = models.DateField(auto_now=True, null=True)

    created_by = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, related_name='tp_created_by') # add)
    updated_by = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, related_name='tp_updated_by') # add) , editable=False

    def __str__ (self):
        return f"{self.training}"

```

Then I created a view in views.py (as shown below) to create be able to load and see data using an html template.

```auto

def trainingProgramed(request):
  trainingprgset = c_training_programed.everything.all().values()
  template = loader.get_template('trainingProgramed.html')

  context = {
    'trainingprgset': trainingprgset,
    }
  #print(context)
  return HttpResponse(template.render(context, request))

```

and finally I created an HTML file to be able to load and see data from the view above “trainingProgramed”

the piece of code of HTML to load the context of the view is below.

```auto
 <tbody>
    {% for training in trainingprgset %}
    <tr>
        <td>{{ training.training.training_name }}</td>
        <td>{{ training.trainer.firstname }}</td>
        <td>{{ training.places }}</td>
        <td>{{ training.places_available }}</td> 
        <td>{{ training.inicial_dt }}</td>
        <td>{{ training.final_dt }}</td>
        <td>{{ training.total_hours }}</td>
        <td>{{ training.created_by_id }}</td>
        <td>{{ training.created_dt }}</td>
        <td>{{ training.updated_by_id }}</td>
        <td>{{ training.last_modified_dt }}</td>
        <td>{{ training.is_deleted }}</td>

         <td class="td-actions text-right">
         <button type="button" rel="tooltip" class="btn btn-info btn-link btn-just-icon btn-sm" data-original-title="" title="">
         <i class="material-icons">person</i>
         </button>
         <button type="button" rel="tooltip" class="btn btn-success btn-link btn-just-icon btn-sm" data-original-title="" title=""> <i class="material-icons">edit</i>
          </button>
                                        
          </td>
      </tr>  
      {% endfor %}
  </tbody>

```

The html using the context = {‘trainingprgset’: trainingprgset,} created in the view above to load and showing data on web browser.

for the first column {{ training.training.training\_name }} I am using the field “training” from table “c\_training\_programed” that is a foreign key field to table “c\_training” to recall the training name, but the issue is the training\_name is not shown on the web broser.

On image below you can see the column Training that is referred to {{ training.training.training\_name }} is not loading the training\_name from c\_training table.

Any advise will be really appreciated.

 ![programed training](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/c/2/c208f0fb36ce83884f6d27e68f4b51d491475c01.png)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 26, 2024, 2:06pm UTC](https://forum.djangoproject.com/t/no-able-to-link-load-data-on-html-from-another-table-using-the-foreign-key/36762/2 "2024-11-26T14:06:20Z")

</div>

The problem is here:

> [@lsaavedra21](#):
>
> ```auto
> trainingprgset = c_training_programed.everything.all().values()
> ^^^^^^
> 
> ```

By having the query return values instead of the objects, you’re breaking the foreign key relationship between the models. The values dict contains the PK of the related model, not a reference to the model itself.

My general recommendation is to _never_ use the `values` function unless it is absolutely necessary. You’re (almost) always better off allowing Django to create the objects for you.  
(There are other ways around this, but as a general rule, you should be allowing the objects to be created.)

Side note: To make things easier for other people when they need to read your code, I strongly suggest you adhere to Django’s coding conventions. In this case, I’m referring to your model names. It will help avoid confusion if your models were named `C_Training_Programmed` and `C_Training`.

---

<div class="post-metadata">

**Author:** ![lsaavedra21](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/lsaavedra21/32/24443_2.png) [@lsaavedra21](https://forum.djangoproject.com/u/lsaavedra21)\
**Post date:** [November 26, 2024, 2:37pm UTC](https://forum.djangoproject.com/t/no-able-to-link-load-data-on-html-from-another-table-using-the-foreign-key/36762/3 "2024-11-26T14:37:22Z")

</div>

Hi Ken,

thanks a lot for your reply, I am clear now. And txs for your recommendation about the use of “values” function and django´s coding convention, I will keep in mind, I think it is important to know the best practices…

Again, thank you so much.

By the way, issue solved 🙂
