Django Query vs SQL query

19 views
Skip to first unread message

Eugene TUYIZERE

unread,
Jun 15, 2021, 4:43:26 AM6/15/21
to django...@googlegroups.com
Team,

I am a little bit confused: I have a table callect Projects and another called Staffs. I want a particular staff member to view only the project he/she has a role in. In the Staffs table I have a project foreign key project_id. But I am confused about how to construct the query. I think of the SQL as below

Select * from Projects where Projects.id = Staffs.projects_id 

I want to transform this query into a django query.

Assist please

--
Eugene

Nikeet NA

unread,
Jun 15, 2021, 5:09:40 AM6/15/21
to Django users
use the related name on the staff model for project field with __ lookup as in 
class Staff(models.Model):
.............
.........
....
project = foreignkey relationship (related_name='staffs')

SO the query would be Projects.objects.filter(staffs__project_id=F('pk'))

Reply all
Reply to author
Forward
0 new messages