0

I have some related models:

class Reward(CommonInfo):
   approved = models.BooleanField(default=False)
   manager = models.ForeignKey(OrganisationUser, related_name='rewards_given') #todo add contraint so that manager should be manager of this role
   approver = models.ForeignKey(OrganisationUser, null=True, related_name='approved_rewards', blank=True)# todo same as above but approver
   number_of_gems = models.PositiveIntegerField(null=True, db_column='number_of_gems', blank=True)
   tag = models.ForeignKey(Tag,related_name='rewards')

class Organisation(CommonInfo):
   ORGANISATION_TYPE = (('C', 'Company'),
                     ('P', 'Product provider'),
                     ('D', 'Demo Company'))
   type_of_organisation = models.CharField(choices=ORGANISATION_TYPE, max_length=1) 
   name = models.CharField(max_length=50)
   organisation_details = models.OneToOneField('OrganisationDetails', null=True, blank=True, related_name='organisation')

class OrganisationUser(CommonInfo):
  TYPE_OF_USER = (('D', 'Demo user'),
                ('R', 'Registered user'))
  type_of_user = models.CharField(choices=TYPE_OF_USER, default='D', max_length=1)
  active = models.BooleanField(default=True)
  user = models.OneToOneField(User, related_name='organisation_user')
  managers = models.ManyToManyField('self', related_name='employees_managed', null=True, default=None, blank=True, symmetrical=False)
  approvers = models.ManyToManyField('self', related_name='approvees', null=True, default=None, blank=True, symmetrical=False)

I need to query to fetch all tags rewarded by a manager belonging to particular company.

I want to do something like this in views.py:

   def get_gems_for_manager_and_peers(self, request, *args, **kwargs):
         org_user = request.user.organisation_user 
         tags = Reward.objects.values('tag').filter(manager__exact=org_user.id,----query to check -manger_belong to particular_organisation---

So, Problem is to find all managers of specific organisations. Or is there any other way to do this.

1 Answer 1

1

I need to query to fetch all tags rewarded by manager belonging to particular company

Maybe I'm misunderstanding you, but this should be pretty straightforward:

org_user = request.user.organisation_user
Tag.objects.filter(rewards__manager=org_user).distinct()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.