简体   繁体   中英

Filtering by id of ToManyField

I have a resource that looks like this:

class CustomerResource(ModelResource):

    locations = fields.ToManyField('device.resources.LocationResource',
            'location_set', null=True)
    current_location = fields.ToOneField('device.resources.LocationResource',
            'current_location', null=True)
    default_location = fields.ToOneField('device.resources.LocationResource',
            'default_location', null=True)

    class Meta:
        queryset = Customer.objects.all()
        resource_name = 'customers'
        validation = CleanedDataFormValidation(form_class=RegistrationForm)
        list_allowed_methods = ['get', 'post']
        detail_allowed_methods = ['get', 'put', 'patch', 'delete']
        authorization = Authorization()
        excludes =['is_superuser', 'is_active', 'is_staff', 'password', 'last_login',]
        filtering = {
            'location': ('exact'),
        }

I want to query the API for a list of customers filtered by whether they have a certain location in their location field. An example url would look like this:

localhost/v1/customers/?location=1&format=json

Unfortunately, while Tastypie recognizes my current filtering scheme as valid, when I pass in the location parameter to the URL, it appears to do nothing. The endpoint returns a list of all customers. Am I doing something wrong, or is there a way to extend filtering to get what I want?

您的ToManyField称为“位置”,而不是“位置”

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