Home > OS >  ''QuerySet' object has no attribute 'enter_the_destination_account_number'
''QuerySet' object has no attribute 'enter_the_destination_account_number'

Time:10-13

Can anyone tell me what's wrong with my code? I am trying to use filter but its showing ''QuerySet' object has no attribute 'enter_the_destination_account_number'. I tried get() but it shows,

get() returned more than one MoneyTransfer -- it returned 14!.

here's some snap of code. Thanks in advance

models.py

    class Status (models.Model):
    user_name = models.CharField(max_length=150, default=None)
    account_number = models.IntegerField()
    balance = models.IntegerField()
    phone_number= models.CharField(max_length=20, default=0)

    class MoneyTransfer(models.Model):
    enter_your_user_name = models.CharField(max_length = 150, default = None)
    enter_the_destination_account_number = models.IntegerField()
    enter_the_destination_phone_number=models.CharField(max_length=20, default=None)
    enter_the_amount_to_be_transferred_in_INR = models.IntegerField()

views.py


    def TransferMoney(request):
    if request.method == "POST":
        form = forms.MoneyTransferForm(request.POST)
        if form.is_valid():
            form.save()

            curr_user = models.MoneyTransfer.objects.filter(enter_your_user_name=request.user)
            dest_user_acc_num = curr_user.enter_the_destination_account_number  #dest_phone number add korte hobe
            dest_phone_num= curr_user.enter_the_destination_phone_number

            temp = curr_user  # NOTE: Delete this instance once money transfer is done

            dest_user = models.Status.objects.get(account_number=dest_user_acc_num)  # FIELD 1
            dest_phn= models.Status.objects.get(phone_number= dest_phone_num)
            transfer_amount = curr_user.enter_the_amount_to_be_transferred_in_INR  # FIELD 2
            curr_user = models.Status.objects.get(user_name=request.user)  # FIELD 3

            # Now transfer the money!
            curr_user.balance = curr_user.balance - transfer_amount
            #dest_phn.balance = dest_phn.balance   transfer_amount
            dest_user.balance = dest_user.balance   transfer_amount

            # Save the changes before redirecting
            curr_user.save()
            dest_user.save()

            temp.delete()  # NOTE: Now deleting the instance for future money transactions

        return redirect(index)
    else:
        form = forms.MoneyTransferForm()
    return render(request, "epayapp/Transfer_money.html", {"form": form})

CodePudding user response:

The issue is that filter returns a queryset, not an object. So you cannot access the fields directly from the queryset, what you can do is grab the first element from the queryset and perform the operations you want. Like this: curr_user = models.MoneyTransfer.objects.filter(enter_your_user_name=request.user).first() So it'll look like this:

def TransferMoney(request):
    if request.method == "POST":
        form = forms.MoneyTransferForm(request.POST)
        if form.is_valid():
            form.save()
            # Grab the first object from the queryset
            curr_user = models.MoneyTransfer.objects.filter(enter_your_user_name=request.user).first()
       
            dest_user_acc_num = curr_user.enter_the_destination_account_number  #dest_phone number add korte hobe
            dest_phone_num= curr_user.enter_the_destination_phone_number

            temp = curr_user  # NOTE: Delete this instance once money transfer is done

            dest_user = models.Status.objects.get(account_number=dest_user_acc_num)  # FIELD 1
            dest_phn= models.Status.objects.get(phone_number= dest_phone_num)
            transfer_amount = curr_user.enter_the_amount_to_be_transferred_in_INR  # FIELD 2
            curr_user = models.Status.objects.get(user_name=request.user)  # FIELD 3

            # Now transfer the money!
            curr_user.balance = curr_user.balance - transfer_amount
            #dest_phn.balance = dest_phn.balance   transfer_amount
            dest_user.balance = dest_user.balance   transfer_amount

            # Save the changes before redirecting
            curr_user.save()
            dest_user.save()

            temp.delete()  # NOTE: Now deleting the instance for future money transactions

        return redirect(index)
    else:
        form = forms.MoneyTransferForm()
    return render(request, "epayapp/Transfer_money.html", {"form": form})

CodePudding user response:

the filter method on objects does not returned an object as you think. it will return a Queryset object

You have to loop into the results of filter or take an item with first() function for example

def TransferMoney(request):
    if request.method == "POST":
        form = forms.MoneyTransferForm(request.POST)
        if form.is_valid():
            form.save()

            queryset = models.MoneyTransfer.objects.filter(enter_your_user_name=request.user)
            curr_user = queryset.first()

            dest_user_acc_num = curr_user.enter_the_destination_account_number  #dest_phone number add korte hobe
            dest_phone_num= curr_user.enter_the_destination_phone_number

            temp = curr_user  # NOTE: Delete this instance once money transfer is done

            dest_user = models.Status.objects.get(account_number=dest_user_acc_num)  # FIELD 1
            dest_phn= models.Status.objects.get(phone_number= dest_phone_num)
            transfer_amount = curr_user.enter_the_amount_to_be_transferred_in_INR  # FIELD 2
            curr_user = models.Status.objects.get(user_name=request.user)  # FIELD 3

            # Now transfer the money!
            curr_user.balance = curr_user.balance - transfer_amount
            #dest_phn.balance = dest_phn.balance   transfer_amount
            dest_user.balance = dest_user.balance   transfer_amount

            # Save the changes before redirecting
            curr_user.save()
            dest_user.save()

            temp.delete()  # NOTE: Now deleting the instance for future money transactions

        return redirect(index)
    else:
        form = forms.MoneyTransferForm()
    return render(request, "epayapp/Transfer_money.html", {"form": form})

get function have to returned only one element or it will raises a exception... Maybe set account_number and user_name unique in your fields definition for rpeventing this cases

  • Related