B
B
bituke2021-05-07 07:15:18
Django
bituke, 2021-05-07 07:15:18

How to filter objects of one model by filtering another model in django?

There is a model extending the User model:

class Profile(models.Model):
  '''additional parameters to the user'''
  user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
  teamleader = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, related_name='workers_of_group', null=True) #user teamleader

There is a model with data for this user:
class TestData(models.Model):
  '''data table'''
  worker = models.ForeignKey(User, on_delete=models.CASCADE, related_name='worker',)
  data1 = models.IntegerField()
  data2 = models.IntegerField()

Now, in views.py, we need to get the data of all the workers that are assigned to the current teamleader. How can you get it?
I tried to get them through Profile, through User, through TestData, but nothing worked out for me.
Once again, you need to get all the objects of the TestData model, where the worker as user is assigned to the current teamleader, which is assigned to the Profile model. Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2021-05-07
@bituke

TestData.objects.filter(worker__profile__teamleader=u)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question