35👍
Using select_related() will pre-populate the appropriate attributes:
Employee.objects.select_related()
20👍
It is an old question, let me provide a new answer.
Actually, you can do this:
employees = Employee.objects.all().values('id','name','company__name')
then, Django will automatically lookup Company class and find the company name for you.
on the template page, use {{employees.company__name}} then it will display the company name correctly.
- How can I disable a model field in a django form
- Getting "This field is required" even though file is uploaded
- Spurious newlines added in Django management commands
10👍
I guess what you’re looking for is the select_related method of your queryset.
See the doc
select_related()
Returns a QuerySet that will
automatically “follow” foreign-key
relationships, selecting that
additional related-object data when it
executes its query. This is a
performance booster which results in
(sometimes much) larger queries but
means later use of foreign-key
relationships won’t require database
queries
- Django bootstrap alerts not working as expected
- Return list of objects as dictionary with keys as the objects id with django rest framerwork
- How to display "This many months ago" in Django using Humanize?
2👍
With raw queries
qry1 = "SELECT c.car_name, p.p_amount FROM pay p, cars c where p.user_id=%s;"
cars = Cars.objects.raw(qry1, [user_id])
- Running collectstatic on server : AttributeError: 'PosixPath' object has no attribute 'startswith'
- How to setup SysLogHandler with Django 1.3 logging dictionary configuration
- Graphene-python performance issues for large data sets