Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialcsj
12,678 Points[Django] Explain to me like I'm five - reserve lookup that span relationships
Can someone please explain to me like I'm five how and what is going when you want to making a reserve lookup that span relationships? Or recommend a certain video(s). I'm completely stuck in relationships and how pull data out of database.
I've been reading trough https://docs.djangoproject.com/en/1.10/topics/db/queries/#lookups-that-span-relationships and ListView documentation. But I can't adapt what I read to my own needs. So I conclude that I don't understand what I'm reading.
I need to learn how to get a hold project.title
and project.featured_image
of App 1's Project model inside App 2's template HTML. And I've set App 2's model the a ForeignKey to Project. But that isn't enough, so either something isn't right with the map/views.py or I'm not drilling down correctly inside the map_list.html.
I've just left "title": "{{ project.title }}",
for reference. I've tried a bunch of things, all kind of variations of for loops. In the style of for mapdata in project.map.all
and mapdata.title
etc.
Here is the setup:
App 1: projects
projects/models.py
from django.db import models
class Project(models.Model):
title = models.CharField(max_length=200)
text = models.TextField()
date = models.DateField()
featured_image = models.ImageField(
upload_to='projects/featured', blank=True)
projects/views.py
from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Project
from .models import ProjectImage
class ProjectListView(ListView):
model = Project
class ProjectDetailView(DetailView):
model = Project
projects/template/projects/project_list.html (every get outputted as expected)
{% for project in object_list %}
<div class="card {{ project.title }} {{ project.get_typology_display }}">
<div class="featured_image">
<img src="{{ MEDIA_URL }}/media/{{ project.featured_image }}" alt="{{ project.title }}" />
</div>
<h4><a href="{% url 'projects:project-detail' project.pk %}">{{ project.title }}</a></h4>
<h5>{{ project.get_typology_display }}</h5>
</div>
{% endfor %}
App 2: map
map/models.py
from django.db import models
from projects.models import Project
class Map(models.Model):
project = models.ForeignKey(Project, related_name='map')
lon = models.DecimalField(max_digits=10, decimal_places=8, default=1)
lat = models.DecimalField(max_digits=10, decimal_places=8, default=1)
location = models.CharField(max_length=200, blank=True)
map/views.py
from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Map
from projects.models import Project
class MapListView(ListView):
model = Map
Snippet from map/template/map/map_list.html (map.lon and map.lat works)
var markers = {
type: 'FeatureCollection',
features: [
{% for map in object_list %}
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
{{ map.lon }},
{{ map.lat }}
]
},
"properties": {
"title": "{{ project.title }}",
"marker-symbol": "marker"
}
},
{% endfor %}
]
};
I know this is perhaps convoluted. Sorry.
2 Answers
jacinator
11,936 Pointsproject would now be an attribute of Map
, so to access it in the context data you should refer to it by {{ map.project.title }}
. I've modified the code below. Give that a try and good luck!
var markers = {
type: 'FeatureCollection',
features: [
{% for map in object_list %}
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
{{ map.lon }},
{{ map.lat }}
]
},
"properties": {
"title": "{{ map.project.title }}", // Try changing this line like so
"marker-symbol": "marker"
}
},
{% endfor %}
]
};
csj
12,678 PointsYou're turning out to be my savior... Although I'm embarrassed, I'm sure I try the line out... Or I didn't :)... Perhaps it was inside an invalid loop I tried it. I get so confused sometimes, I can't remember what and how. Trying something new from the docs, I can completely loss my head.
jacinator
11,936 PointsBeen there, done that. :)