Question

I've seen some examples of how to do this on SO but none of them have guided me towards the glory I so desire.

Here are the fields I'm working with:

models.py

class ServerFunctions(models.Model):
    server_function = models.CharField(max_length=12)

    class Meta:
        verbose_name_plural = "Server Function"

    def __unicode__(self):
        return self.server_function

class Inventory(models.Model):
    server_function = models.ForeignKey(ServerFunctions, null=False, blank=False)

views.py

def show_details(request, host_id=1):
    host_info = Inventory.objects.filter(id=host_id).values()
    return render_to_response('templates/details.html', {'host_info': host_info})

templates/details.html

This gives me the column value from the Inventory table (3) like it should

{{ info.server_function_id }}

This gives me no output at all.

{% for func in info.serverfunctions_set.all %}
    {{ func.server_function }}
{% endfor %}

I'm stuck, nothing I've tried seems to work. Thanks for reading.

Was it helpful?

Solution

You are using info.serverfunction_set.all in your template which would be correct if you were traversing a reverse relationship (i.e. you had a ServerFunction and wanted all the related Inventory items). But you aren't, you have a simple forward relationship between Inventory and a ServerFunction:

{% for info in host_info %}
    {{ func.server_function.server_function }}
{% endfor %}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top