Ilya Bibik
Ilya Bibik

Reputation: 4124

Django model managers -object is not callable

First time I am using the managers in django model so problem can be very basic

I try to calculate totals for my Materials model those totals are stored in different tables like Inventory , PO, SO

For this reason I decided to do the aggregations inside Material model manager

class MaterialInventoryManager(models.Manager):
    def total_active_inventory(self):
        return self.get_query_set().annotate(total_inventory=Sum('inventory__quantity')).filter(is_active = True)


class Material(models.Model):
    version = IntegerVersionField( )
    code = models.CharField(max_length=30)
    name = models.CharField(max_length=30)
    description = models.TextField(null=True, blank=True)
    materialuom = models.CharField(max_length=1,
                              choices=UOM_CHOICES)
    creation_time = models.DateTimeField(auto_now_add=True, blank=True)
    total_inventory =  MaterialInventoryManager()
    objects = MaterialInventoryManager()


@with_author 
class Inventory(models.Model): 
    material =  models.OneToOneField('item.Material',null=True, blank=True)
    # material_UOM =  models.OneToOneField('item.UOM_BINUOM',null=True, blank=True)
    warehouse_Bin =  models.ForeignKey(WarehouseBin)
    is_active = models.BooleanField(default=True)
    quantity =  models.DecimalField(max_digits=8, decimal_places=5)
    creation_time = models.DateTimeField(auto_now_add=True, blank=True)

And when I execute from shell Material.total_inventory() I am getting enter image description here

What I am doing wrong?

Upvotes: 0

Views: 2348

Answers (1)

Shang Wang
Shang Wang

Reputation: 25539

You have typo, it's self.get_queryset() not self.get_query_set().

Django doc about get_queryset().

Upvotes: 3

Related Questions