Home > Mobile >  getting data from the backend django models
getting data from the backend django models

Time:11-27

I am working on a project pastly the i used sql querys, but change in the django models the models i used are for the members table

class Members(models.Model):
    name = models.CharField(max_length = 50)
    address = models.CharField(max_length = 50)
    phoneNo = models.CharField(unique=True, max_length = 50)
    created = models.DateTimeField(auto_now_add=True)
    update = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

for the bills table

salesPerson = models.ForeignKey(User, on_delete = models.SET_NULL, null=True)
    purchasedPerson = models.ForeignKey(Members, on_delete = models.SET_NULL, null=True)
    cash = models.BooleanField(default=True)
    totalAmount = models.IntegerField()
    advance = models.IntegerField(null=True, blank=True)
    remarks = models.CharField(max_length = 200, null=True, blank=True)
    created = models.DateTimeField(auto_now_add=True)
    update = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ['-update', '-created']

i am having a query that is

credits = Bills.objects.all().filter(cash = False).values ('purchasedPerson').annotate(
        cnt = Count('purchasedPerson'),
        debit = Sum( 'totalAmount' ),
        credit = Sum( 'advance' ),
        balance = Sum( 'totalAmount' ) - Sum( 'advance' )
    ).order_by ('purchasedPerson')

getting the output as

[{'purchasedPerson': 1, 'cnt': 2, 'debit': 23392, 'credit': 100, 'balance': 23292}, {'purchasedPerson': 2, 'cnt': 1, 'debit': 1280, 'credit': 0, 'balance': 1280}]

but i need in the out put name, address and phone number also how can i get those

CodePudding user response:

Pass all fields you need in .values() like:

Bills.objects.filter(cash = False).values('purchasedPerson', 'name', 'address', 'phoneNo').annotate(...)

Another choice is skipping the .values() part:

Bills.objects.filter(cash = False).annotate(...)

Note: all() is not required in your case.

  • Related