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

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

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}"

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.


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.

 <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.

The problem is here:

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.

1 Like

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 :slight_smile: