Comforti Chambeshi
Comforti Chambeshi

Reputation: 139

'QuerySet' object has no attribute 'user'

I am trying to query from pending payments and then when a user requests for a payment i also want to be checking from the orders that corresponds to a logged in user. This is the code that fetches the withdraw requests:

pending_requests = WithdrawRequest.objects.filter(status='pending')

Code that fetches the totals orders corresponding to the user from pending_requests variable above which is pending_requests.user:

 #amount query
    AccBalance = OrderData.objects.filter(payment_to=pending_requests.user, payment_status='approved').aggregate(totals=Sum(F('item__price')*F('quantity')))

The problem is i am getting this error:

'QuerySet' object has no attribute 'user'

My full view.py code:

def withdraw_requests(request):
pending_requests = WithdrawRequest.objects.filter(status='pending')
#amount query
AccBalance = OrderData.objects.filter(payment_to=pending_requests.user, payment_status='approved').aggregate(totals=Sum(F('item__price')*F('quantity')))

context = {'AccBalance':AccBalance,'pending_requests':pending_requests}
return render(request, 'accounts/withdraw_requests.html', context)

models.py:

class Order(models.Model):
    STATUS = (
      ('pending', 'pending'),
            ('delivered', 'delivered'),
      ('collected', 'collected'),
            ('progressing', 'progressing'),
      ('await', 'await'),
            
            ) 


    OPTIONS = (
            ('card', 'card'),
            ('momo', 'momo'),
            
            ) 
   
    ORDERTYPE = (
            ('single', 'single'),
            ('bulk', 'bulk'),
            
            )   
   
   
    
    user_id = models.ForeignKey(User, on_delete=models.CASCADE)
    status = models.CharField(max_length=250, null=True, choices=STATUS)
    txn_track = models.CharField(max_length=800, blank=False, unique=True)
    payment_method = models.CharField(max_length=250, choices=OPTIONS)
    order_type = models.CharField(max_length=250, default='', choices=ORDERTYPE)
    added_date = models.DateTimeField(auto_now_add=True)

    
     
    def __str__(self):
      return self.txn_track


class OrderData(models.Model):
  PAYMENTSTATUS = (
            ('withdrawn', 'withdrawn'),
            ('rejected', 'rejected'),
      ('approved', 'approved'),
      ('pending', 'pending'),
            
            )  
  item = models.ForeignKey(Item,  on_delete=models.CASCADE)
  order = models.ForeignKey(Order, on_delete=models.CASCADE)
  quantity = models.IntegerField()
  user = models.ForeignKey(User, on_delete=models.CASCADE)
  payment_to = models.ForeignKey(BusinessAccount, on_delete=models.CASCADE, null=True)
  payment_status = models.TextField(choices=PAYMENTSTATUS, default='approved')
  def __str__(self):
      return self.item.name

class WithdrawRequest(models.Model):
   STATUS = (
      ('pending', 'pending'),
            ('rejected', 'rejected'),
      ('approved', 'approved'),
            
            )  
   user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True,)
   business = models.ForeignKey(BusinessAccount,blank=True,  default='', null=True, on_delete=models.CASCADE)
   status = models.CharField(choices=STATUS, max_length=250, blank=True, default='pending')
   proceeded_by = models.IntegerField(blank=True,  default=0)
   account_id = models.CharField(max_length=500)
   payment_method = models.ForeignKey(PaymentMethod, on_delete=models.CASCADE)
   date_time = models.DateField( auto_now_add=True)

   def __str__(self):
       return self.user.first_name+' '+self.user.last_name+ ' ('+self.payment_method.name+'='+ self.account_id+')'
   

Upvotes: 0

Views: 174

Answers (1)

Brad Martsberger
Brad Martsberger

Reputation: 1967

pending_requests is a QuerySet of (possibly many) WithdrawRequest objects. It is not a single WithdrawRequest and doesn't have a .user attribute. So you can't do pending_requests.user. You can get a collection of all the users on the individual instances:

pending_request_users = [pending_request.user for pending_request in pending_requests]

And you can use this to filter your OrderData

AccBalance = OrderData.objects.filter(
    payment_to__in=pending_request_users,
    payment_status='approved'
).aggregate(totals=Sum(F('item__price')*F('quantity')))

Upvotes: 1

Related Questions