3

I created a Company in my django app, two or more person can login with the same company. I want to show the data of one user of the company to the other user of the company.

To simplify: If one user of a company creates an object, then it should be visible to all the users of that company

But I am having trouble extracting the data

Models.py

class ItemBatch(models.Model):
    uploaded_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='uploaded_by')
    name = models.CharField(max_length=100)
    pid = models.IntegerField(blank=True)
    quantity = models.IntegerField(blank=True)

class Teacher(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
    company = models.ForeignKey(Company, on_delete=models.CASCADE, related_name='ship_company')


class User(AbstractUser):
    is_supplier = models.BooleanField(default=False)
    is_teacher = models.BooleanField(default=False)
    email = models.EmailField(default=False)

class Company(models.Model):
    company_name = models.CharField(max_length=255, default=0)
    company_email = models.EmailField(max_length=255, default=0)
    company_phone = models.CharField(max_length=255, default=0)
    company_code = models.IntegerField(default=0)

Views.py

class allordersview(viewsets.GenericViewSet, mixins.ListModelMixin):
  queryset = ItemBatch.objects.all()
  serializer_class = holdSerializer

  def list(self, request, *args, **kwargs):

      queryset = self.filter_queryset(self.get_queryset())

      queryset = queryset.filter(uploaded_by_id=request.user) # ??Changes?? 
      .
      .
      return Response(serializer.data)

How to change the queryset such that it shows all ItemBatch of a Company ??

0

1 Answer 1

3

You can do

queryset.filter(uploaded_by__teacher__company=request.user.teacher.company)

PS: Instead of overriding list method override get_queryset method and apply filter there like

def get_queryset(self):
    return ItemBatch.objects.filter(uploaded_by__teacher__company=self.request.user.teacher.company)
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.