I am trying to display data from different models in my template and I have no idea why it doesn't work properly.
Models.py:
class Company(models.Model):
name = models.CharField(max_length=100)
class Location(models.Model):
company = models.ForeignKey('Company',
related_name='locations')
Views.py:
def sth(request):
loc_list = Location.objects.all()
return render(request, 'blabla/index.html', {'loc_list': loc_list})
Template:
{% for loc in loc_list %}
{% for entry in loc.company_set.all %}
{{entry.name}}
{% endfor %}
{% endfor %}
Why the name of the company is not showing?
You foreign key relationship is the wrong way around... the way you have currently defined them, a Location can have only one Company associated with it, but in your template you are trying to fetch a list of Companys.
For your template logic to work, your models would have to be defined as follows:
class Company(models.Model):
name = models.CharField(max_length=100)
location = models.ForeignKey('Location')
class Location(models.Model):
# You probably need some other fields in here.
Then this would work:
{% for loc in loc_list %}
{% for entry in loc.company_set.all %}
{{entry.name}}
{% endfor %}
{% endfor %}
Only use "_set" when accessing backwards relationships. There is no
loc.company_set.all
because each location only has one company (that is what a ForeignKey is. You can access the location's company by doing loc.company). If you want location to have multiple companies, either see solarissmoke's answer (where you put the ForeignKey attribute on Company and not on Location), or use a ManyToManyField relationship (this will allow companies to have multiple locations and each location to have multiple companies).
Then you can access all the companies of a given location by doing this in the template:
{% for loc in loc_list %}
{% for entry in loc.company.all %}
{{entry.name}}
{% endfor %}
{% endfor %}
You can also access all the locations for a given company by using the related_name (company.locations).
Short answer:
{% for loc in loc_list %}
{{loc.company.name}}
{% endfor %}
Related
I have an object that contains a Many-to-Many field. I'm trying to iterate this field in Django template, but apparently I can't. Let me show you the code first.
models.py:
class Book(models.Model):
title = models.CharField(max_length = 100, blank=True)
category = models.ManyToManyField(Category)
def __str__(self):
return self.title
views.py:
def book_list(request):
books = Book.objects.all().order_by('-pk')
context = {
'books' : books,
}
return render(request, 'contents/book_list.html', context)
Template file:
{% for b in books %}
<div>
{{b.title}}
{% for cat in b.category %}
{{cat}}
{% endfor %}
</div>
{% endfor %}
Now I get 'ManyRelatedManager' object is not iterable error. How do I iterate the field and show all the category in each object?
It's because if you call b.category it returns only the relation object. To get its values (category objects) you have to add .all. Like this:
{% for b in books %}
<div>
{{ b.title }}
{% for cat in b.category.all %}
{{cat}}
{% endfor %}
</div>
{% endfor %}
By the way, I've also changed c.title to b.title, because I assume you want this book title, not something from global.
I'm trying to make individual pages for each author showing their name and posts. I can't seem to get the username displayed.
views.py
class UserProfileView(generic.ListView):
template_name = 'howl/user-profile.html'
context_object_name = 'user_howls'
def get_queryset(self):
author = self.request.user
u = User.objects.get(username=author)
return Howl.objects.filter(author=u)
models.py
class Howl(models.Model):
author = models.ForeignKey(User, null=True)
content = models.CharField(max_length=150)
Here is where I'm stuck.
user-profile.html
{% extends 'howl/base.html' %}
{% block content %}
<h1>User: {{user_howl.author}}</h1>
{% for user_howl in user_howls %}
<ul>
<li>{{user_howl.content}}</li>
</ul>
{% endfor %}
{% endblock %}
The content is displayed just fine, but the heading just says "User: ", how do I give it a context without using a for loop?
I've tried:
{% for author in user_howls.author %}
<h1>User: {{author}}</h1>
{% endfor %}
and
{% if user_howls.author %}
<h1>User: {{user_howl.author}}</h1>
{% endif %}
Still the same outcome, displaying "User: "
user_howls is a queryset so it won't have an author attribute, you need to get the author of the iterated object
{% for howl in user_howls %}
<h1>User: {{ howl.author}}</h1>
{% endfor %}
More to the point though, it doesn't make sense to start from a Howl list, when you are just returning the results for the user_profile, nor does it make sense to use a ListView. so instead, start from the user and then look up its howls
user_obj.howl_set.all()
Since your queryset is based on the posts belonging to the current user, you can shortcut all of this and just show the user directly:
User: {{ user }}
I have two models — Note and Pinboard — with a many to many relationship in a Django app. Those two models are related trough another model — Pin — so I can store additional information about the relationship.
I want to show the related Note instances in the DetailView for Pinboard. That's not the problem. But I want to prefetch the notes and also order them on the position field from the through table.
Any hints on how to archive this (prefetch + ordering on third table)?
Example
This is what I have so far… it works in the sense, that I don't have to query for each entry, but I found no way to order the Note instances by their position without more queries for each instance.
Models
from django.db import models
class Note(models.Model):
title = models.CharField(max_lenght=200)
content = models.TextField()
class Pinboard(models.Model):
title = models.CharField(max_lenght=200)
notes = models.ManyToManyField(
Note, blank=True, related_name='pinboards', through='Pin'
)
class Pin(models.Model):
class Meta:
ordering = ['position', ]
get_latest_by = 'position'
pinboard = models.ForeignKey(Pinboard, related_name='note_pins')
note = models.ForeignKey(Note, related_name='pinboard_pins')
position = models.PositiveSmallIntegerField(default=0)
View
from django.views.generic import DetailView
class PinboardDetailView(DetailView):
model = Pinboard
queryset = Pinboard.objects.prefetch_related('notes')
Template
{% extends 'base.html' %}
{% block content %}
<h1>{{ pinboard.title }}</h1>
{% if pinboard.notes.all.exists %}
<ol>
{% for note in pinboard.notes %}
<li>{{ note.title }}</li>
{% endfor %}
</ol>
{% else %}
<p>Nothing here yet…</p>
{% endif %}
{% endblock content %}
I suggest you use a Prefetch object.
class PinboardDetailView(DetailView):
model = Pinboard
queryset = Pinboard.objects.prefetch_related(
Prefetch(
'notes',
Note.objects.order_by('pinboard_pins__position'),
)
)
By the way, you don't need to use prefetch_related at all on a DetailView as it will result in the same number of queries.
Plus, since you're already fetching the pinboard.notes I suggest you use {% if pinboard.notes.all %} instead of {% if pinboard.notes.all.exists %}.
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.
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 %}
i have 3 models:
Book, Topiccenter and EntryBook.
these are model definitions:
class Book(models.Model):
title = models.TextField()
language = models.TextField()
class Topiccenter(models.Model):
title = models.TextField():
description = models.TextField()
class EntryBook(models.Model):
book = models.ForeignKey(Book,related_name="b_entries")
topiccenter = models.ForeignKey(Topiccenter,related_name="tc_books")
Now I am in Topiccenter T. and i search for books and get all books in DB. As you see, each book can be in multiple topiccenters.
What i want to do is, in the search result, i want to show whether each book is contained in current Topiccenter or not:
I will take all books books = Book.objects.all() and the current topiccenter as tc and render them to template and in template,
{% for book in books %}
{% for entry in book.b_entries.all %}
{% if entry.topiccenter.id == tc.id %}
already in this Topiccenter
{% else %}
add to this topiccenter
{% endif %}
{% endfor %}
{% endfor %}
but the problem is that one book is in two topiccenters and in template i am getting both already in this Topiccenter and add to this topiccenter which is nonesense. How can i repair my logic so that I can check if the book in this current topiccenter, and if not, show them add button
thanks
See how you can move it to the view. In this case, get all books associated with the tc and send that in the context.
Now, the template logic would be:
{% for book in books %}
{% if book in tc_books %}
already in this Topiccenter
{% else %}
add to this topiccenter
{% endif %}
{% endfor %}
Where (in the view)
tc_books = Books.objects.filter(b_entries__topiccenter = tc)
and send that in the context