I have this model:
class Country(models.Model):
name=models.CharField(max_length=100,unique=True)
createdon=models.DateTimeField(auto_now_add=True)
updatedon=models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
class Guest(models.model):
nationality=models.ForeignKey(Country,related_name='guest-nation')
in my views.py, I do this:
guest=Guest.objects.all().values('nationality')
In my template:
{% for info in guest %}
{{ info.nationality }}
{% endfor %}
Now, I am hoping the nationality field to return name of the country but it returns id of the country (as stored in nationality_id) field. From the documentation, I understood it is supposed to return the value specified in the str method.
I can access the name if i the values is nationality__name. It looks uglier than I thought and I am somehow convinced it is supposed to call the str method. Am I wrong?
Try this:
{% for info in guest %}
{{ info.nationality.name }}
{% endfor %}