[Answer]-How to find the name of the user with whom the file is shared?

1👍

Add the condition of a particular file name in the second queries:

shared_username = User.objects.filter(id__in = Share.objects.filter(users_id = log_id, files__file_name='file1').values_list('shared_user_id', flat=True))


However your model has some problems worth noting:

1- If you replace Share.shared_user_id with a OnetoMany field your model would be more correct and your queries a lot easier.

2- If you use related_name on your foreign keys you can access them from the opposite model and your queries would be much easier.

3- You should not use plural var names for foreign keys as they represent one object. File.users should be File.user and the same for Share.files and Share.users

4- Your model could be a lot more readable if you name your vars properly. For example instead of File.users you could use File.owner or File.owning_user. Same with Share.users: Share.sharing_user.

Leave a comment