Django Python Foreign Key issue

Question:

I am showing "well" information linked to a specific project successfully based on well_id
When I try to show drilling_tools in a similar fashion I get an error.
Can anyone see what I am doing wrong in my views?

def well_show(request, well_id):
    well = Well.objects.get(pk=well_id)
    drilling_tools = DrillingTool.objects.get(pk=well_id)
    return render(request, 'geodata/well_show.html', {'well': well, 'drilling_tools': drilling_tools})
Asked By: LoudEye

||

Answers:

Try this instead if they are related:

def well_show(request, well_id):
    well = Well.objects.get(pk=well_id)
    drilling_tools = DrillingTool.objects.filter(well__id=well_id)
    return render(request, 'geodata/well_show.html', {'well': well, 'drilling_tools': drilling_tools})

Your code isn’t working because the well.id can differ from the drilling_tools.id. They are in a separate table after all, and some funky (but easily preventable) race conditions can occur.

Answered By: nigel239
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.