# Select only foreign key column and querry set

**URL:** <https://forum.djangoproject.com/t/select-only-foreign-key-column-and-querry-set/6015>\
**Category:** Using Django\
**Created:** [January 6, 2021, 8:16pm UTC](https://forum.djangoproject.com/t/select-only-foreign-key-column-and-querry-set/6015 "2021-01-06T20:16:17Z")\
**Posts on this page:** 15\
**Page:** 2

<div class="post-metadata">

**Author:** ![Maciej](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/maciej/32/2984_2.png) [@Maciej](https://forum.djangoproject.com/u/Maciej)\
**Post date:** [January 9, 2021, 7:05pm UTC](https://forum.djangoproject.com/t/select-only-foreign-key-column-and-querry-set/6015/21 "2021-01-09T19:05:30Z")

</div>

Clarification that you request:  
Airport → Airport\_Frequnecy thi is one to many. One airport can have a lot of freq.

Airport list view should show a list of airports with ICAO. Airport Details will show all Airport fields details + frequency correspondingto the airport  
Frequency list view should show ICAO and airport names with link (under ICAO) to Frequency detail list for particular aitrport.

> [@KenWhitesell](#):
>
> You’re always calling get\_absolute\_url on an instance of an object, it doesn’t matter what the object type is.

so it means when i’m callin get\_absolute\_url on airport object ( whicch is a result of qurery in Airport\_Frequency in this case) → called get\_absolute\_url will be the one from it’s model (Airport in this case)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 9, 2021, 7:30pm UTC](https://forum.djangoproject.com/t/select-only-foreign-key-column-and-querry-set/6015/22 "2021-01-09T19:30:08Z")

</div>

I’m sorry, I’m getting confused when you make multiple references to different pages. I’m trying to focus in on the one specific page for which you’re identifying this issue. I understand that you may have different questions on your different views, but let’s try to address these one at a time.

My assumption from the view names is that we’re currently looking to address this one:

> [@Maciej](#):
>
> Frequency list view should show ICAO and airport names with link (under ICAO) to Frequency detail list for particular airport.

So to repeat my earlier questions:

- What is this to be a list of? (Airport, or Airport\_Frequency?)
- For each entry in this list, what data do you want displayed?
- What do you want to get a URL for? (I’m guessing a detailed page for either Airport or Airport\_Frequency, which one?)

---

<div class="post-metadata">

**Author:** ![Maciej](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/maciej/32/2984_2.png) [@Maciej](https://forum.djangoproject.com/u/Maciej)\
**Post date:** [January 9, 2021, 7:54pm UTC](https://forum.djangoproject.com/t/select-only-foreign-key-column-and-querry-set/6015/23 "2021-01-09T19:54:35Z")

</div>

> [@KenWhitesell](#):
>
> - What is this to be a list of? (Airport, or Airport\_Frequency?)

Airports - but only ones which are in Frequency table (not all have freq)

> [@KenWhitesell](#):
>
> - For each entry in this list, what data do you want displayed?

Depending on plase you are:

- if on the Airport list then it airport detailed data and frequency (all details)
- if on the freq list then airport ICAO only and frequencies

> [@KenWhitesell](#):
>
> - What do you want to get a URL for? (I’m guessing a detailed page for either Airport or Airport\_Frequency, which one?)

- two diffrent ones one is pointing to Airport details second to Frequency details

Hope it is clear now

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 10, 2021, 1:43am UTC](https://forum.djangoproject.com/t/select-only-foreign-key-column-and-querry-set/6015/24 "2021-01-10T01:43:37Z")

</div>

I think we’re close, but then you write:

> [@Maciej](#):
>
> Depending on plase you are:
> 
> - if on the Airport list then it airport detailed data and frequency (all details)
> - if on the freq list then airport ICAO only and frequencies

What do you mean by “Depending on the place you are”?  
This is an airport list. What frequency list are you referring to?

Do you have a mock-up or sample of what this page is supposed to look like?

---

<div class="post-metadata">

**Author:** ![Maciej](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/maciej/32/2984_2.png) [@Maciej](https://forum.djangoproject.com/u/Maciej)\
**Post date:** [January 10, 2021, 9:44am UTC](https://forum.djangoproject.com/t/select-only-foreign-key-column-and-querry-set/6015/25 "2021-01-10T09:44:37Z")

</div>

> [@KenWhitesell](#):
>
> Do you have a mock-up or sample of what this page is supposed to look like?

 ![Airport_list_views.PNG](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/1/1454919836a2e67ce58d4ae5d932c24998294b6e.jpeg)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 10, 2021, 1:50pm UTC](https://forum.djangoproject.com/t/select-only-foreign-key-column-and-querry-set/6015/26 "2021-01-10T13:50:15Z")

</div>

That’s a nice site layout and explains the direction you’re going. But again, to avoid confusion, I’m trying to focus on just one page, the page that started this discussion. So to clarify for this one specific view that we’re talking about here, is this the page we’re discussing for this issue?  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/4/44fb92d4511b92c351c2ff73462b52175e070fa8.png)

---

<div class="post-metadata">

**Author:** ![Maciej](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/maciej/32/2984_2.png) [@Maciej](https://forum.djangoproject.com/u/Maciej)\
**Post date:** [January 10, 2021, 2:55pm UTC](https://forum.djangoproject.com/t/select-only-foreign-key-column-and-querry-set/6015/27 "2021-01-10T14:55:00Z")

</div>

Indeed. And this page shows only airports that have a freq. It works fine with custom query that you share at the begining of our discussion. However the issue is now the link (blue fonts in the picture)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 10, 2021, 4:35pm UTC](https://forum.djangoproject.com/t/select-only-foreign-key-column-and-querry-set/6015/28 "2021-01-10T16:35:05Z")

</div>

Cool. So, assuming that your detail page (what the links refer to) is a named url, perhaps something like  
`path('frequency_list/<int:airport_id>/', FrequencyListForAirport.as_view(), name='airport-frequency-list')`,  
Then your template can render that part of the line as something like:  
`<a href="{% reverse "airport-frequency-list" airport.id %}>{{airport.icao}}</a>`  
(Typing this off-the-cuff - there may be an error/typo or two - hopefully this is close enough to give you some ideas.)

---

<div class="post-metadata">

**Author:** ![Maciej](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/maciej/32/2984_2.png) [@Maciej](https://forum.djangoproject.com/u/Maciej)\
**Post date:** [January 10, 2021, 11:04pm UTC](https://forum.djangoproject.com/t/select-only-foreign-key-column-and-querry-set/6015/29 "2021-01-10T23:04:46Z")

</div>

Thanks url works now fine - i made some changes

url.py:

```
re_path(r'^frequency/(?P<pk>\d+)$', Airport_FrequencyDetailView.as_view(), name='frequency-detail'),

```

template:

```
a href="{% url 'airports:frequency-detail' airportfrequency.ID %}">{{airportfrequency.ICAO}}</a...
```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 10, 2021, 11:22pm UTC](https://forum.djangoproject.com/t/select-only-foreign-key-column-and-querry-set/6015/30 "2021-01-10T23:22:17Z")

</div>

Cool! Glad to see you have it working.

> [@Maciej](#):
>
> `re_path(r'^frequency/(?P<pk>\d+)`, Airport\_FrequencyDetailView.as\_view(), name=‘frequency-detail’),`

I know it’s mostly a matter of personal taste, but I don’t see the value of using a regex for the path here. I find `path('frequency/<int:pk>', ...` to be far more readable.

> [@Maciej](#):
>
> `{{airportfrequency.ICAO}}`

I thought ICAO was an attribute of an Airport and not of Airport\_Frequency? (At least, that’s the case in your model at the top of this thread.)

---

<div class="post-metadata">

**Author:** ![Maciej](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/maciej/32/2984_2.png) [@Maciej](https://forum.djangoproject.com/u/Maciej)\
**Post date:** [January 22, 2021, 10:29pm UTC](https://forum.djangoproject.com/t/select-only-foreign-key-column-and-querry-set/6015/31 "2021-01-22T22:29:58Z")

</div>

Hi,

So far so good however I face another issue.

In my model Frequency is many to one model. I defined a custom querry to get airport frequencies basing on airport name:

```
def get_queryset(self):
   frequency_list = Airport_Frequency.objects.all()
   return frequency_list.filter(airport_id = self.kwargs['airport_id'])

```

so I get following view:

 ![model](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/1/1173910178bef6b8fb7d23884ac0a155a5201e08.png)

Now I trying to figrueout the way to get airport name by using airport\_id (here is many to one relation)  
I was trying:

```
 airport_frequency.airport.all

```

in tamplate to refer to the model (airport\_frequency → airport\_id is a foreign key to airport model)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 22, 2021, 10:48pm UTC](https://forum.djangoproject.com/t/select-only-foreign-key-column-and-querry-set/6015/32 "2021-01-22T22:48:11Z")

</div>

The `name` field within the `Airport` object would be just a direct field reference:  
`airport_frequency.airport.name`

---

<div class="post-metadata">

**Author:** ![Maciej](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/maciej/32/2984_2.png) [@Maciej](https://forum.djangoproject.com/u/Maciej)\
**Post date:** [January 22, 2021, 11:09pm UTC](https://forum.djangoproject.com/t/select-only-foreign-key-column-and-querry-set/6015/33 "2021-01-22T23:09:08Z")

</div>

Great, it works when i iterate through an object:

```
{% for af in airport_frequency_list %}
    <tr>
    ...
    <td>{{af.airport.Name}}</td>
    <td>{{af.airport.ICAO}}</td>
    </tr>
{% endfor%}

```

It works fine, however just an extra question:

If all freq. concern to an airport ( the same id in airport ID column), how can refer to airport id once for example if I would like to place airport name before " Frequency details" header. e.g.:

```
{{airport.Name}} Frequency details"

```

.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 22, 2021, 11:33pm UTC](https://forum.djangoproject.com/t/select-only-foreign-key-column-and-querry-set/6015/34 "2021-01-22T23:33:25Z")

</div>

You’ve got a couple different options - use whatever makes sense to you.

1. Get the airport name in the view and stash it in the context and just refer to that variable in your template.
2. See the [forloop.first](https://docs.djangoproject.com/en/3.1/ref/templates/builtins/#for) option.
3. Treat airport\_frequency\_list as a list and reference the `0` index. (`airport_frequency_list.0`)

(Note - I’ve used the first two options, I’ve never tried the third.)

---

<div class="post-metadata">

**Author:** ![Maciej](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/maciej/32/2984_2.png) [@Maciej](https://forum.djangoproject.com/u/Maciej)\
**Post date:** [January 23, 2021, 10:53am UTC](https://forum.djangoproject.com/t/select-only-foreign-key-column-and-querry-set/6015/35 "2021-01-23T10:53:06Z")

</div>

Thanks, works perfectly

> def get\_context\_data(self, \*\*kwargs):  
> context = super().get\_context\_data(\*\*kwargs)  
> context[‘airportData’] = Airport.objects.filter(ID = self.kwargs[‘airport\_id’])  
> return context

[Previous page](https://forum.djangoproject.com/t/select-only-foreign-key-column-and-querry-set/6015.md?page=1)
