Look up field from a different Django model given another model field - python

I have two models in Django: Room for offices, and Person for employees. One office could have multiple employees. I'm trying to make a detail-view html page that shows a person's details, like their name and office number. I can get the details from the Person model fine, but I'm having trouble doing a reverse look-up to the Room model. How can I get a person's office given the following code?
#models.py
class Room(models.Model):
number = models.CharField('Room number', unique=True)
persons = models.ManyToManyField('Person', blank=True)
#...
class Person(models.Model):
name = models.CharField('Full name', max_length=200)
#...
#views.py
from django.views import generic
class PersonDetailView(generic.DetailView):
model = Person
#person_detail.html
{% extends "base_generic.html" %}
{% block content %}
<h1>Name: {{ person }}</h1>
<p>Room: {{ Room.number }}</p>
{% endblock %}
All that currently does is return a name, but "Room" is left blank.

You could overload the get_context_data method
class PersonDetailView(DetailView):
model = Person
context_object_name = 'person'
template_name = 'person_detail.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# woops, typo
# context['room'] = Room.objects.filter(person=context['object']).first()
context['room'] = Room.objects.filter(persons=context['object']).first()
return context
And the template:
#person_detail.html
{% extends "base_generic.html" %}
{% block content %}
<h1>Name: {{ person }}</h1>
<p>Room: {{ room.number }}</p>
{% endblock content %}
Edit:
But you might be better off with Person having a models.ForeignKey pointing to a Room, so a Person can only have one Room, but many Person can have the same Room.
You could then keep your orginal view and change your template:
#person_detail.html
{% extends "base_generic.html" %}
{% block content %}
<h1>Name: {{ person }}</h1>
<p>Room: {{ person.room.number }}</p>
{% endblock content %}

Related

How do you get queryset objects related to queryset passed to templates in Django

I have these two models and as you can see they have a relationship.
class Post(models.Model):
text = models.TextField()
class PostImage(models.Model):
post = models.ForeignKey(Post, default=None, on_delete=models.CASCADE)
image = models.FileField(upload_to = 'media/',blank=True, null=True)
As far as I understand if I query posts and push them to a template and post, I would expect to use something like this in my templates to retrieve the images URL attached to the posts but it doesn't seem to work.
{% for post in posts %}
{% for post_image in post.post_image_set.all %}
{{post_image.image.url}}
{% endfor %}
{% endfor %}
What am I doing wrong?
Here is my views.py file.
views.py
# Create your views here.
def index(request):
posts=Post.objects.filter(approved=True).order_by('-published_date')
context = {"posts":posts}
return render(request, 'post/home.html',context)
The default related name for a foreign key relational is the name of the model (PostImage) but in your template for loop you called it post_image Following relationships “backward”
change
{% for post_image in post.post_image_set.all %}
into
{% for post_image in post.postimage_set.all %}
Template code (with change)
{% for post in posts %}
{% for post_image in post.postimage_set.all %}
{{post_image.image.url}}
{% endfor %}
{% endfor %}

How to access model.field in template? django

I started learning django few days ago and trying to build my first blog.
My problem is that I decided to add an extra field for my categories (subheading), which I want to be in my template, but can't understand how to do it.
my models.py
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=20)
subheading = models.CharField(max_length=160)
def __str__(self):
return self.name
class Post(models.Model):
title = models.CharField(max_length=255)
body = models.TextField()
link = models.TextField()
categories = models.ManyToManyField("Category", related_name="posts")
def __str__(self):
return self.title
views.py
from django.shortcuts import render
from blog.models import Post, Category
def blog_category(request, category):
posts = Post.objects.filter(
categories__name__contains=category
).order_by(
'title'
)
context = {
"category": category,
"posts": posts
}
return render(request, "blog_category.html", context)
The only way category.name or category.subheading are displayed in template (by the teacher) is inside {% for post in posts %} {% endfor %}:
{% for post in posts %}
{% for category in post.categories.all %}
{{ category.subheading }}
{% endfor %}
{% endfor %}
In this case, if there are 10 posts on category page, subheading repeats 10 times. I only need to print 1 to describe category.
Is there a way to call category.subheading outside of {% for post in posts %} ? Or somehow to print only one result.
p.s. sorry for my primitive English level.
You can do this with a Prefetch object [Django-doc]:
from django.db.models import Prefetch
def blog_category(request, category):
posts = Post.objects.filter(
categories__name__contains=category
).prefetch_related(
Prefetch(
'categories',
Category.objects.filter(name__contains=category)
to_attr='relevant_categories'
)
).order_by(
'title'
)
# …
In your template, you can then render this with:
{% for post in posts %}
{% for category in post.relevant_categories %}
{{ category.subheading }}
{% endfor %}
{% endfor %}
Not sure to understand what you want to do but you can search and access to Category elements by doing something like that:
categories=Category.objects.filter(name='NameYouWanttoSearch').values_list('subheading')
can add a model manager to the categories , take single instance and call it in templates instead of all.
class CategoryManager(models.Manager):
def single_category(self):
return self.get_queryset()[:1]
class Category(models.Model):
name = models.CharField(max_length=20)
subheading = models.CharField(max_length=160)
objects = CategoryManager()
def __str__(self):
return self.name
and in templates
{% for post in posts %}
{% for category in post.categories.single_category %}
{{ category.subheading }}
{% endfor %}
{% endfor %}

Django _set.all filter not working in template

I'm trying to filter a list of objects in my database but I can't get it to work on the template using _set.all. The strange thing is it's something I've done in two other places in my project but I cant see why it isnt working this time.
view.py:
class GolfMonthlyView(generic.ListView):
template_name="monthly_view/golf-monthly-view.html"
context_object_name='golf_monthly_view'
queryset = GolfMonthlyView.objects.all()
def get_context_data(self, **kwargs):
context = super(GolfMonthlyView, self).get_context_data(**kwargs)
context['golftour'] = golf_models.Tour.objects.all()
context['golftournament'] = golf_models.Tournament.objects.all()
models:
class Tour(models.Model):
name = models.CharField(max_length=100)
class Tournament(models.Model):
tour = models.ForeignKey('Tour', on_delete=models.CASCADE)
name = models.CharField(max_length=100)
template html:
{% for info in golf_monthly_view %}
{% for competition in golftour %}
{% for golftournament in golftour.golftournament_set.all %}
<ul>
<li>{{golftournament.name}}</li>
</ul>
{% endfor %}
{% endfor %}
{% endfor %}
The good news out of all this is in trying to work out my problem its forced me to use the Django Shell for the first time properly. So I know the relationship is there and functional, it just isnt displaying in the template.
Edit: The working thing:
class RugbyMonthlyView(generic.ListView):
template_name="monthly_view/rugby-monthly-view.html"
context_object_name='rugby_monthly_view'
queryset = RugbyMonthlyView.objects.all()
def get_context_data(self, **kwargs):
context = super(RugbyMonthlyView, self).get_context_data(**kwargs)
context['competition'] = rugby_models.Competition.objects.all()
context['match'] = rugby_models.Match.objects.all()
return context
model.py:
class Competition(models.Model):
name = models.CharField(max_length=200)
class Match(models.Model):
competition = models.ForeignKey('Competition', on_delete=models.CASCADE)
html template:
{% for match_info in rugby_monthly_view %}
{% for competition in competition %}
*code*
{% for match in competition.match_set.all %}
*code*
{% endfor %}
{% endfor %}
{% endfor %}
You have golftour.golftournament_set.all nested in a loop on the context list golf_monthly_view (not sure why you're doing this), which I think is empty because the ListView QuerySet is wrong:
queryset = GolfMonthlyView.objects.all()
# ^^^^ ?? This is not a model
If you yank off the outer for loop for example, the inner loops should proceed if the QuerySets are not empty:
{% for competition in golftour %}
{% for golftournament in golftour.tournament_set.all %}
<ul>
<li>{{golftournament.name}}</li>
</ul>
{% endfor %}
{% endfor %}

django categories: How to get children of a category using django-categories?

I am using django-categories to implement music related app. I want artist as my category and his/her songs as children
models.py
from django.db import models
from django_extensions.db.fields import AutoSlugField
from categories.models import CategoryBase
class Artist(CategoryBase):
cat = models.CharField(max_length=255, blank=True)
def __unicode__(self):
return self.name
class Song(models.Model):
title = models.CharField(max_length=255,)
slug = AutoSlugField(populate_from='title', unique=True)
description = models.TextField()
cat = models.ForeignKey(Artist, blank=True)
def __unicode__(self):
return self.title
In templates, artist_details.html
{% extends 'base_post.html' %}
{% load category_tags %}
{% block page_content %}
<h1>{{ artist.name }}</h1>
{% if artist.children.count %}
<h2>Subcategories</h2>
<ul>
{% for child in artist.children.all %}
<li>{{ child }}</li>
{% endfor %}
</ul>
{% endif %}
The template is getting rendered coz I can see artist's name. But i am unable to fetch the children. I checked the docs but I could not find much stuff related to fetching children.
There is data for both models in my DB, I added relevant info via admin interface. Can anyone tell me what I am missing ?
Also I open to using better packages. You can give any suggestions that implements categories.
SOLUTION: From django docs https://docs.djangoproject.com/en/1.6/topics/templates/#accessing-method-calls
thanks mariodev
Even using django-categories, you can't have songs as children of artists. Artists just do not form a category.
What you instead want is something like this:
from django.db import models
from django_extensions.db.fields import AutoSlugField
from categories.models import CategoryBase
class MusicCategory(CategoryBase):
# add extra fields, like images, "featured" and such here
pass
class Artist(CategoryBase):
name = CharField(max_length=255,)
categories = models.ManyToManyField(MusicCategory, related_name="artists")
def __unicode__(self):
return self.name
class Song(models.Model):
slug = AutoSlugField(populate_from='title', unique=True)
title = models.CharField(max_length=255,)
artist = models.ForeignKey(Artist, related_name="songs", on_delete=models.PROTECT)
categories = models.ManyToManyField(MusicCategory, related_name="songs")
description = models.TextField()
def __unicode__(self):
return self.title
and with some templates
{% extends 'base_post.html' %}
{% load category_tags %}
{% block page_content %}
<h1>{{ artist.name }}</h1>
{% if artist.songs.all.exists %}
<h2>Songs</h2>
<ul>
{% for song in artist.songs.all %}
<li>{{ song }}</li>
{% endfor %}
</ul>
{% endif %}
REF: https://django-categories.readthedocs.org/en/latest/custom_categories.html#creating-custom-categories

Django template: access query_set and non-query_set results in same template

I have a Django app that contains info on schools and states. I want my template to display a list of schools per state and also the name of the state based on the state parameter in the URL. So if a user goes to example.com/vermont/ they will see a list of Vermont schools and a tag that says they're on the "Vermont" page. I can get the list of schools per state to work, but I can't figure out how to simply list the state name in the h1 tag.
Here is my models.py:
from django.db import models
class School(models.Model):
school_name = models.CharField(max_length=200)
location_state = models.CharField(max_length=20)
def __unicode__(self):
return self.school_name
Here is my views.py:
from django.views.generic import ListView
class StateListView(ListView):
model = School
template_name = 'state.html'
context_object_name = 'schools_by_state'
def get_queryset(self):
state_list = self.kwargs['location_state']
return School.objects.filter(location_state=state_list)
And here's my template for state.html:
{% extends 'base.html' %}
{% block content %}
<h1>{{school.location_state }}</h1> [THIS IS THE LINE THAT DOES NOT WORK]
{% for school in schools_by_state %}
<ul>
<li>{{ school.school_name }}</li>
</ul>
{% endfor %}
{% endblock content %}
What am I missing here?
The problem is that the school variable never enters the context. You are only setting the schools_by_state to the context.
To add some extra context you need to override the get_context_data method. This way you can add the location_state from the url parameter:
def get_context_data(self, **kwargs):
context = super(StateListView, self).get_context_data(**kwargs)
context.update({'state': self.kwargs['location_state']})
return context
Then you can use the {{ state }} instead of {{ school.location_state }} in your template.

Categories