[Fixed]-Create Django Queryset from Model and ForeignKey

1👍

It’s not clear to me what you’re trying to do from your question and your view code confuses things further as you assign the result of Plaque.objects.all() to two different variables and then put them together. The ForeignKey objects on the Plaque model will all be available in the queryset. You can use select_related to make sure you pull them all back efficiently, like so:

plaques = Plaque.objects.select_related("veteran").all()
for p in plaques:
    print p.veteran

N.B., the .all() isn’t strictly necessary but I wanted to make it clear I was using the same query as you and simply adding to it.

👤Tom

0👍

You should be able to use the select_related method.

queryset_list = Plaque.objects.select_related('Veteran').all().order_by('first_name')

The detail is provided in the Django documentation.

Leave a comment