简体   繁体   中英

Need help in resolving the error 'The QuerySet value for an exact lookup must be limited to one result using slicing'

I am relatively new to django and I have got an error which I am unable to understand. I have 3 models namely customer, services and uses. I want to fetch the services used by a customer. I am doing this via the uses model by querying the models(specifically using the filter method). However I am getting the following error

ValueError at /employee/view_customer/ The QuerySet value for an exact lookup must be limited to one result using slicing.

the customer model contains all the details of the customers .The services model contains the details about the services(name,price). The uses model contains 2 foreign keys (customer,service). It basically stores the services used by Here are my models

Customer
class Customer(models.Model):
    firstname = models.CharField(max_length=15)
    lastname = models.CharField(max_length=15)
    age = models.IntegerField()
    sex = models.CharField(max_length=10)
    phoneno = models.IntegerField()
    emailid = models.CharField(max_length=25)
    address = models.CharField(max_length=50)
    children = models.IntegerField()
    adults = models.IntegerField()
    roomtype = models.CharField(max_length=10)
    aadharno = models.CharField(max_length=15)
    daysstayed = models.IntegerField()
    date_visited = models.DateTimeField(default=timezone.localtime())

    def __str__(self):
        if self.firstname == None:
            return ''
        else:
            return str(self.id)

Services
class Services(models.Model):
    service_name = models.CharField(max_length=15)
    price = models.IntegerField()
    

    def __str__(self):
        if self.service_name == None:
            return ''
        else:   
            return str(self.service_name)
Uses
class Uses(models.Model):
    customer = models.ForeignKey(Customer,on_delete=CASCADE)
    service_id = models.ForeignKey(Services,on_delete=CASCADE)
    time_used = models.TimeField(default=timezone.localtime())

    def __str__(self):
        if self.customer == None:
            return ''
        else:   
            return str(self.customer)

The function at employee/view_customer

def view_customer(request):
    if request.method == 'POST':
        customer = Customer.objects.first()
        uses = Uses.objects.filter(customer_id=customer)
        services = Services.objects.filter(id=uses)
        print(services)
        return render(request,'employee/cust-info.html',{'customer':customer,'services':services}) 
    else:
     return render(request,'employee/cust-info.html')

I am view_customer to get all the services used by a customer. uses = Uses.objects.filter(customer_id=customer) is used to fetch all the rows where a particular customer is found. services = Services.objects.filter(id=uses) is used to get all the services pertaining to the customer.

I am using django version 3.0.7 and python version 3.7.7. I Need some help to fix this error, thank you.

You can use below code

uses = Uses.objects.filter(customer=customer)    
services = Services.objects.filter(uses__in=uses)

It is also possible to do the above in one shot

services = Services.objects.filter(uses__customer=customer)

You can read more about filtering in the documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM