0

I have created two tables in Sqlite one have the main experience data and other have the details of the data as

class experience(models.Model):
  e_name = models.CharField(max_length=30)
  e_post = models.CharField(max_length=50)
  e_startdate = models.CharField(max_length=30)
  e_enddate = models.CharField(max_length=30)

def __str__(self):
    return self.e_name

class experiencedetail(models.Model):
  e_name = models.ForeignKey(experience,on_delete=models.DO_NOTHING)
  e_detail = models.CharField(max_length=100)

i am trying to shoe the details as shown in this image

my Html code is

    {% for exp in experience %}
    <div id="{{exp.e_name}}" class="tabcontent">
            <h2 class="hf">{{exp.e_post}} @ <span>{{ exp.e_name }}</span></h2>
            <p>{{exp.e_startdate}} - {{exp.e_enddate}}- {{exp.id}}</p>
            <div class="exp-description">
                <h4>
                    <ul style="list-style: square;">
                        {% for expd in exp_detail %}
                            {% if 'expd.e_name' == 'exp.e_name' %}
                                <li>{{ expd.e_detail }}</li>
                            {% endif %}
                        {% endfor %}
                    </ul>
                </h4>
            </div>
        </div>
    {% endfor %}

but this expd.details does not show any data. The data is present in the database.

1 Answers1

1

That is because exp_detail does not exists. You access the reversed ForeignKey manager with:

{% for expd in exp.experiencedetail_set.all %}
    <li>{{ expd.e_detail }}</li>
{% endfor %}

The name of the reversed manager is the value for the related_name=… parameter [Django-doc]. If you do not specify this parameter in your ForeignKey, it will use modelname_set.


Note: Models in Django are written in PerlCase, not snake_case, so you might want to rename the model from experiencedetail to ExperienceDetail.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555