Select only foreign key column and querry set

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.

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)

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:

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?)

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

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
  • two diffrent ones one is pointing to Airport details second to Frequency details

Hope it is clear now

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

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?

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

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)

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.)

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...

Cool! Glad to see you have it working.

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.

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.)

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:

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)

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

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"

.

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 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.)

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