Home > Enterprise >  What changes I should make change the get to filter?
What changes I should make change the get to filter?

Time:02-12

I am doing a project for my college work. In that, I need to change a function. That function is giving error saying multiple objects returned. The function works well if there is only one object to return. But I need to change it to return it to return all the objects. So what changes do I need to do. I am attaching the function code. Pls provide the code to modify also. I am just beginner in coding. Thanks

I am using Pycharm, Django and. MySQL to do my project

The problem is obj=booking.objects.get is returning multiple objects and it can't displayed by the function . So I need to get the code to display all objects. I understood that using filter() will solve the problem. But I don't know what are the changes to make and where should I make it. So pls help. I am just a beginner.

    def vpay(request) :
        
        b=request.session['uid']
        ob=TurfManager.objects.get(t_id=b)
        bb=ob.tf_id
        obj=Booking.objects.get(tf_id=bb)
        nn=obj.b_id
    
        o = Payment.objects.filter(book_id=nn)
        context = {
            'obval': o,
        }
class Payment(models.Model):
    p_id = models.AutoField(primary_key=True)
    # u_id = models.IntegerField()
    u=models.ForeignKey(User,to_field='u_id',on_delete=models.CASCADE)
    payment = models.CharField(max_length=50)
    # book_id = models.IntegerField()
    book=models.ForeignKey(Booking,to_field='b_id',on_delete=models.CASCADE)
    upi_id = models.CharField(max_length=50)
class Booking(models.Model):
    b_id = models.AutoField(primary_key=True)
    # u_id = models.IntegerField()
    u=models.ForeignKey(User,to_field='u_id',on_delete=models.CASCADE)
    # tf_id = models.IntegerField()
    tf = models.ForeignKey(TurfLocation, to_field='tf_id', on_delete=models.CASCADE)
    date = models.DateField()
    #timeslot_id = models.CharField(max_length=50)
    timeslot = models.ForeignKey(TimeSlot, to_field='timeslot_id', on_delete=models.CASCADE)
    status = models.CharField(max_length=50)

CodePudding user response:

You can use filter as

    def vpay(request) :
    
        b = request.session['uid']
        ob = TurfManager.objects.get(t_id=b)
        obj = Booking.objects.filter(tf_id=ob.tf_id)
        o = Payment.objects.filter(book_id__in=obj)
        context = {
            'obval': o,
        }
  • Related