'Django admin query sum quantity for each type for each request.user

the issue is that i have 3 users levels 1-superuser 2-jihawi 3-mahali, I would like to separate the veiw from the "Quantite" function by request.user

models.py

class typestock(models.Model):
   Type = models.CharField(max_length=50, blank=True, null=True)
   def __str__(self):
      return self.Type

class stock(models.Model):
   Jihawi = models.ForeignKey(User, on_delete=models.CASCADE, related_name="stockjihawi", editable=True, blank=True, null=True)
   Mahali = models.ForeignKey(User, on_delete=models.CASCADE, related_name="stockmahali", editable=True, blank=True, null=True)
   Date = models.DateField(blank=True, null=True)
   TypeStock = models.ForeignKey(typestock, on_delete=models.CASCADE, editable=True, blank=True, null=True)
   Quantite = models.IntegerField()
   def save(self, *args, **kwargs):
    super(stock, self).save(*args, **kwargs)
    return self.TypeStock

admin.py

class TypeStockAdmin(nested_admin.NestedModelAdmin, ImportExportModelAdmin):
  inlines = [InlineStock]
  list_display = ('Type', 'Quantite')
  list_display_links = ('Type',)
  list_filter = ('Type',)
  search_fields = ('Type',)

def Quantite(self, obj):
    result = stock.objects.filter(TypeStock_id=obj).aggregate(Sum("Quantite"))
    return result["Quantite__sum"]
Quantite.short_description = "Quantité"

how to add this code in the Quantite function:

if request.user.is_superuser:
   stock.objects.filter(StockType_id=obj).aggregate(Sum("Quantity"))
###must view all quantity from stock models###
elif request.user.is_jihawi:
   stock.objects.filter(TypeStock_id=obj).aggregate(Sum("Quantity")) 
###how to add filter Jihawi=request.user (Jihawi from stock models)###
elif request.user.is_mahali:
   stock.objects.filter(StockType_id=obj).aggregate(Sum("Quantity"))
###how to add filter Mahali=request.user (Mahali from stock models)###


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source