Is there a way to override the default List/Form retrival logic

I have a DocType (Department) and another DocType (Paper).

  • Each department has a child table that contains links to users.
  • Each Paper has a link to a department.

I want the users of (Department A) to only see documents that are linked to their department. How can I override the logic for retrieving the data for both LIst and Form of Department DocType.

Ideally, you would set up user permissions so that only users in a particular department can view the data belonging to that department.
https://docs.erpnext.com/docs/v14/user/manual/en/setting-up/users-and-permissions/user-permissions

I think ERPNext automatically creates User Permissions documents whenever the Employee role is given to them (not sure about this though).
So in your case, you would create these User Permission documents whenever users are added or removed in your Department document through a hook.

If you really want to modify the query for fetching a list, you can look into the permission_query_conditions hook. I would recommend just setting up user permission documents whenever the “Department” doctype is changed and users are added/removed.